Sorting arrays

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Sorting arrays

Alex-2
Hi all,
I have a series of file names in an array that I'm trying to sort with imagej macro language.

expt1-c1-f

expt1-c1-t

expt1-c1-v

expt1-c2-f

expt1-c2-t

expt1-c2-v


I'd like to sort the array like this

expt1-c1-t

expt1-c1-v

expt1-c1-f

expt1-c2-t

expt1-c2-v

expt1-c2-f


Further, I'd prefer to be able to specify the order of the sort : e.g. specify [t, v, f] or [f, t, v] or [v, f, t], etc.


Sent from Yahoo Mail for iPhone

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Sorting arrays

LIM Soon Yew John (IMB)
Hi Alex,

You can use control loops and "endsWith" as shown below:

Array1 = newArray("expt-c1-f", "expt-c1-t", "expt-c1-v", "expt-c2-f", "expt-c2-t", "expt-c2-v");
order = newArray("-t","-v", "-f");
Array2 = newArray(6);
counter = 0;
for(index=1; index<3; index++){
for(e=0; e<order.length; e++){
target = d2s(index,0)+order[e];
for(a=0; a<Array1.length; a++){
if(endsWith(Array1[a], target)){
Array2[counter] = Array1[a];
counter +=1;
}
}
}
}
Array.print(Array2);

Best Regards,
John

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Alex
Sent: Thursday, 21 September, 2017 4:58 AM
To: [hidden email]
Subject: Sorting arrays

Hi all,
I have a series of file names in an array that I'm trying to sort with imagej macro language.

expt1-c1-f

expt1-c1-t

expt1-c1-v

expt1-c2-f

expt1-c2-t

expt1-c2-v


I'd like to sort the array like this

expt1-c1-t

expt1-c1-v

expt1-c1-f

expt1-c2-t

expt1-c2-v

expt1-c2-f


Further, I'd prefer to be able to specify the order of the sort : e.g. specify [t, v, f] or [f, t, v] or [v, f, t], etc.


Sent from Yahoo Mail for iPhone

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
This e-mail and any attachments are only for the use of the intended recipient and may contain material that is confidential, privileged and/or protected by the Official Secrets Act. If you are not the intended recipient, please delete it or notify the sender immediately. Please do not copy or use it for any purpose or disclose the contents to any other person.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Sorting arrays

ctrueden
In reply to this post by Alex-2
Hi Alex,

If you use a full-fledged scripting language [1] then you can lean on a
sort function and define your own comparator.

E.g., here is a Groovy script which does what you want:

  data = [
    "expt1-c1-f",
    "expt1-c2-f",
    "expt1-c1-t",
    "expt1-c2-t",
    "expt1-c1-v",
    "expt1-c2-v"
  ]
  println("before: " + data)
  def order(s) {
    return "tvf".indexOf(s[-1])
  }
  data.sort{a,b ->
    if (a[0..a.length()-2] == b[0..b.length()-2])
      return order(a) <=> order(b)
    else
      return a <=> b
  }
  println(" after: " + data)

Regards,
Curtis

[1] https://imagej.net/Scripting#Supported_languages

--
Curtis Rueden
LOCI software architect - https://loci.wisc.edu/software
ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
Did you know ImageJ has a forum? http://forum.imagej.net/


On Wed, Sep 20, 2017 at 3:57 PM, Alex <
[hidden email]> wrote:

> Hi all,
> I have a series of file names in an array that I'm trying to sort with
> imagej macro language.
>
> expt1-c1-f
>
> expt1-c1-t
>
> expt1-c1-v
>
> expt1-c2-f
>
> expt1-c2-t
>
> expt1-c2-v
>
>
> I'd like to sort the array like this
>
> expt1-c1-t
>
> expt1-c1-v
>
> expt1-c1-f
>
> expt1-c2-t
>
> expt1-c2-v
>
> expt1-c2-f
>
>
> Further, I'd prefer to be able to specify the order of the sort : e.g.
> specify [t, v, f] or [f, t, v] or [v, f, t], etc.
>
>
> Sent from Yahoo Mail for iPhone
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Sorting arrays

LIM Soon Yew John (IMB)
In reply to this post by LIM Soon Yew John (IMB)
Hi Alex,

You can add one more “for” loop into the macro and declare the number of “expt” & “c” as shown below.

Array1 = newArray("expt1-c1-f", "expt1-c1-t", "expt1-c1-v", "expt1-c2-f", "expt1-c2-t", "expt1-c2-v","expt2-c1-f", "expt2-c1-t", "expt2-c1-v", "expt2-c2-f", "expt2-c2-t", "expt2-c2-v");
numExpt = 2; //Number of experiment
numC= 3; //Number of c
order = newArray("-t","-v", "-f");
size = Array1.length;
Array2 = newArray(size);
counter = 0;
for(expt=1; expt<=numExpt; expt++){
for(c=1; c<=numC; c++){
for(e=0; e<order.length; e++){
target = "expt"+d2s(expt,0)+"-c"+d2s(c,0)+order[e];
for(a=0; a<Array1.length; a++){
if(endsWith(Array1[a], target)){
Array2[counter] = Array1[a];
counter +=1;
}
}
}
}
}
Array.print(Array2);

Regards,
John

From: Alex [mailto:[hidden email]]
Sent: Sunday, 15 October, 2017 6:48 AM
To: [hidden email]; [hidden email]; LIM Soon Yew John <[hidden email]>
Subject: Re: Sorting arrays

Just to be clearer ...

Array1 = newArray("expt1-c1-f", "expt1-c1-t", "expt1-c1-v", "expt1-c2-f", "expt1-c2-t", "expt1-c2-v","expt2-c1-f", "expt2-c1-t", "expt2-c1-v", "expt2-c2-f", "expt2-c2-t", "expt2-c2-v");
order = newArray("-t","-v", "-f");
Array2 = newArray(12);
counter = 0;
for(index=1; index<3; index++){
for(e=0; e<order.length; e++){
target = d2s(index,0)+order[e];
for(a=0; a<Array1.length; a++){
if(endsWith(Array1[a], target)){
Array2[counter] = Array1[a];
counter +=1;
}
}
}
}
Array.print(Array2);

yeilds

expt1-c1-t, expt2-c1-t, expt1-c1-v, expt2-c1-v, expt1-c1-f, expt2-c1-f, expt1-c2-t, expt2-c2-t, expt1-c2-v, expt2-c2-v, expt1-c2-f, expt2-c2-f

I'd like to return

expt1-c1-t, expt1-c1-v, expt1-c1-f, expt1-c2-t, expt1-c2-v, expt1-c2-f, expt2-c1-t, expt2-c1-v, expt2-c1-f, expt2-c2-t, expt2-c2-v, expt2-c2-f

Thanks
Alex

On Saturday, October 14, 2017, 3:39:55 PM PDT, Alex <[hidden email]<mailto:[hidden email]>> wrote:


John,

This works great, but it does fail when I have more than one experiment ... for example ...

expt1-c1-t
expt1-c1-v
expt1-c1-f
expt1-c2-t
expt1-c2-v
expt1-c2-f
expt2-c1-t
expt2-c1-v
expt2-c1-f
expt2-c2-t
expt2-c2-v
expt2-c2-f

gets sorted as  :

expt1-c1-t
expt2-c1-t
expt1-c1-v
expt2-c1-v
expt1-c1-f
expt2-c1-f
expt1-c2-t
expt2-c2-t
expt1-c2-v
expt2-c2-v
expt1-c2-f
expt2-c2-f

Any thoughts?








On Wednesday, September 20, 2017, 7:58:37 PM PDT, LIM Soon Yew John <[hidden email]<mailto:[hidden email]>> wrote:


Hi Alex,

You can use control loops and "endsWith" as shown below:

Array1 = newArray("expt-c1-f", "expt-c1-t", "expt-c1-v", "expt-c2-f", "expt-c2-t", "expt-c2-v");
order = newArray("-t","-v", "-f");
Array2 = newArray(6);
counter = 0;
for(index=1; index<3; index++){
for(e=0; e<order.length; e++){
target = d2s(index,0)+order[e];
for(a=0; a<Array1.length; a++){
if(endsWith(Array1[a], target)){
Array2[counter] = Array1[a];
counter +=1;
}
}
}
}
Array.print(Array2);

Best Regards,
John

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]<mailto:[hidden email]>] On Behalf Of Alex
Sent: Thursday, 21 September, 2017 4:58 AM
To: [hidden email]<mailto:[hidden email]>
Subject: Sorting arrays

Hi all,
I have a series of file names in an array that I'm trying to sort with imagej macro language.

expt1-c1-f

expt1-c1-t

expt1-c1-v

expt1-c2-f

expt1-c2-t

expt1-c2-v


I'd like to sort the array like this

expt1-c1-t

expt1-c1-v

expt1-c1-f

expt1-c2-t

expt1-c2-v

expt1-c2-f


Further, I'd prefer to be able to specify the order of the sort : e.g. specify [t, v, f] or [f, t, v] or [v, f, t], etc.


Sent from Yahoo Mail for iPhone

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
This e-mail and any attachments are only for the use of the intended recipient and may contain material that is confidential, privileged and/or protected by the Official Secrets Act. If you are not the intended recipient, please delete it or notify the sender immediately. Please do not copy or use it for any purpose or disclose the contents to any other person.


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
This e-mail and any attachments are only for the use of the intended recipient and may contain material that is confidential, privileged and/or protected by the Official Secrets Act. If you are not the intended recipient, please delete it or notify the sender immediately. Please do not copy or use it for any purpose or disclose the contents to any other person.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html