Login  Register

Re: Sorting arrays

Posted by ctrueden on Sep 21, 2017; 9:51am
URL: http://imagej.273.s1.nabble.com/Sorting-arrays-tp5019415p5019418.html

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