Sorting an Array according to a sub-string

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

Sorting an Array according to a sub-string

Pascal Lorentz-2
Dear list

I am working on a macro and struggle with sorting a file list.

I have a file list like this:
channels und stacks.lif_TileScan 2_t0_s0_z00_ch00.tif
channels und stacks.lif_TileScan 2_t0_s0_z00_ch01.tif
channels und stacks.lif_TileScan 2_t0_s0_z01_ch00.tif
channels und stacks.lif_TileScan 2_t0_s0_z01_ch01.tif
channels und stacks.lif_TileScan 2_t0_s0_z02_ch00.tif
channels und stacks.lif_TileScan 2_t0_s0_z02_ch01.tif
channels und stacks.lif_TileScan 2_t0_s1_z00_ch00.tif
channels und stacks.lif_TileScan 2_t0_s1_z00_ch01.tif
channels und stacks.lif_TileScan 2_t0_s1_z01_ch00.tif
channels und stacks.lif_TileScan 2_t0_s1_z01_ch01.tif
channels und stacks.lif_TileScan 2_t0_s1_z02_ch00.tif
channels und stacks.lif_TileScan 2_t0_s1_z02_ch01.tif
channels und stacks.lif_TileScan 2_t0_s2_z00_ch00.tif
channels und stacks.lif_TileScan 2_t0_s2_z00_ch01.tif
channels und stacks.lif_TileScan 2_t0_s2_z01_ch00.tif
channels und stacks.lif_TileScan 2_t0_s2_z01_ch01.tif
channels und stacks.lif_TileScan 2_t0_s2_z02_ch00.tif
channels und stacks.lif_TileScan 2_t0_s2_z02_ch01.tif
channels und stacks.lif_TileScan 2_t1_s0_z00_ch00.tif
channels und stacks.lif_TileScan 2_t1_s0_z00_ch01.tif
channels und stacks.lif_TileScan 2_t1_s0_z01_ch00.tif
channels und stacks.lif_TileScan 2_t1_s0_z01_ch01.tif
channels und stacks.lif_TileScan 2_t1_s0_z02_ch00.tif
channels und stacks.lif_TileScan 2_t1_s0_z02_ch01.tif
...
I read these files into an array. If I sort the array it is of course
sorted by _t0, _t1, _t2 etc. However I would like to sort it by _s0,
_s1. _s2 instead. I was wondering if there is a direct way to sort an
array according to a specific sub-string. I read out all the indices in
order to be able to extract a sub-string.
Alternatively could I maybe cut out the _t1 sub-string and add it at the
end of the filename to get _s0_z00_ch00_t0.tif and then resort the
array? I already tried this but was not able to do it for a whole array.
Could somebody help me with this?

Any help is highly appreciated

Best regards

Pascal

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

Re: Sorting an Array according to a sub-string

Herbie
Good day Pascal,

not sure if this is the most elegant way but these macro code lines will
do the conversion for a single file name (watch for line breaks):

fName = "channels und stacks.lif_TileScan 2_t0_s0_z00_ch00.tif";
print(fName);
idx = indexOf(fName, "_t"); // start
strA = split(substring(fName, idx+3),".");
fName = substring(fName, 0, idx)+strA[0]+substring(fName, idx,
idx+3)+"."+strA[1]; // end
print(fName);

You need too loop through the elements of your array and put the three
central macro lines in this loop.

HTH

Herbie

::::::::::::::::::::::::::::::::::::::::::::
Am 26.04.18 um 10:12 schrieb Pascal Lorentz:

> Dear list
>
> I am working on a macro and struggle with sorting a file list.
>
> I have a file list like this:
> channels und stacks.lif_TileScan 2_t0_s0_z00_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s0_z00_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s0_z01_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s0_z01_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s0_z02_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s0_z02_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s1_z00_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s1_z00_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s1_z01_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s1_z01_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s1_z02_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s1_z02_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s2_z00_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s2_z00_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s2_z01_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s2_z01_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s2_z02_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s2_z02_ch01.tif
> channels und stacks.lif_TileScan 2_t1_s0_z00_ch00.tif
> channels und stacks.lif_TileScan 2_t1_s0_z00_ch01.tif
> channels und stacks.lif_TileScan 2_t1_s0_z01_ch00.tif
> channels und stacks.lif_TileScan 2_t1_s0_z01_ch01.tif
> channels und stacks.lif_TileScan 2_t1_s0_z02_ch00.tif
> channels und stacks.lif_TileScan 2_t1_s0_z02_ch01.tif
> ...
> I read these files into an array. If I sort the array it is of course
> sorted by _t0, _t1, _t2 etc. However I would like to sort it by _s0,
> _s1. _s2 instead. I was wondering if there is a direct way to sort an
> array according to a specific sub-string. I read out all the indices in
> order to be able to extract a sub-string.
> Alternatively could I maybe cut out the _t1 sub-string and add it at the
> end of the filename to get _s0_z00_ch00_t0.tif and then resort the
> array? I already tried this but was not able to do it for a whole array.
> Could somebody help me with this?
>
> Any help is highly appreciated
>
> Best regards
>
> Pascal
>
> --
> 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 an Array according to a sub-string

Jan Eglinger
In reply to this post by Pascal Lorentz-2
Hi Pascal,

If you're up to using some scripting language [1] other than IJ1 Macro,
you could do the following (mind the line breaks introduced by the mailer):

* in Groovy:

array = array.sort{a,b -> a.substring(a.indexOf("_s")) <=>
b.substring(b.indexOf("_s"))}
array.each {println it}


* in Python:

sorted_array = sorted(array, key=lambda x: x.split('_s')[-1])
print ("\n".join(sorted_array))


Hope that helps.
Jan


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


On 26.04.2018 10:12, Pascal Lorentz wrote:

> Dear list
>
> I am working on a macro and struggle with sorting a file list.
>
> I have a file list like this:
> channels und stacks.lif_TileScan 2_t0_s0_z00_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s0_z00_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s0_z01_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s0_z01_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s0_z02_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s0_z02_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s1_z00_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s1_z00_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s1_z01_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s1_z01_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s1_z02_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s1_z02_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s2_z00_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s2_z00_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s2_z01_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s2_z01_ch01.tif
> channels und stacks.lif_TileScan 2_t0_s2_z02_ch00.tif
> channels und stacks.lif_TileScan 2_t0_s2_z02_ch01.tif
> channels und stacks.lif_TileScan 2_t1_s0_z00_ch00.tif
> channels und stacks.lif_TileScan 2_t1_s0_z00_ch01.tif
> channels und stacks.lif_TileScan 2_t1_s0_z01_ch00.tif
> channels und stacks.lif_TileScan 2_t1_s0_z01_ch01.tif
> channels und stacks.lif_TileScan 2_t1_s0_z02_ch00.tif
> channels und stacks.lif_TileScan 2_t1_s0_z02_ch01.tif
> ...
> I read these files into an array. If I sort the array it is of course
> sorted by _t0, _t1, _t2 etc. However I would like to sort it by _s0,
> _s1. _s2 instead. I was wondering if there is a direct way to sort an
> array according to a specific sub-string. I read out all the indices in
> order to be able to extract a sub-string.
> Alternatively could I maybe cut out the _t1 sub-string and add it at the
> end of the filename to get _s0_z00_ch00_t0.tif and then resort the
> array? I already tried this but was not able to do it for a whole array.
> Could somebody help me with this?
>
> Any help is highly appreciated
>
> Best regards
>
> Pascal
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

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