Array.sort(array) macro & How to sort file names containing numbers and strings

Hello, I’ve just started using ImageJ (Fiji, actually) a couple of weeks ago and trying to understand how its macro works (also new field to me). Could someone tell me if “Array.sort” function can only work when all file names in an array only contain either numbers or strings? I mean, it can work for “001.tif, 002.tif, 003.tif” or “aaa.tif, bbb.tif, ccc.tif” but NOT for “aaa_001.tif, aaa_002.tif, aaa_003.tif”?

I saw information at the following link but wasn’t sure if I understood correctly.
https://imagej.nih.gov/ij/developer/macro/functions.html

I asked this because I’m trying to understand batch processing scripts, whose open examples often contain Array.sort macro.

Like this one:

However, my default file names are like “aaa_ch01, aaa_ch02, bbb_ch01, bbb_ch02, ccc_ch01, ccc_ch02, …” where ch01 and ch02 represents different detectors on each image. And so far I couldn’t find a solution to sort these files properly in an array (made from getFileList macro).

Of course I can assemble them in different folders at first and change file names so that they only contain numbers or strings. But I was curious if there’s any smarter way…

Thanks in advance for any help!

@Okap

So I don’t know off-hand myself… but you can test this by writing some print() statements … Sort your list of files with Array.sort() and then print off each name of each file before/after sorting to ensure you are getting what you need.

Give it a try - and if it’s still not working - let us know.

eta

EDIT: tried to clarify the source of confusion for the original poster

Array.sort() should work for any file name – it will sort alphabetically.

Possibly, the confusion came from reading this documentation:

Array.sort(array) - Sorts array , which must contain all numbers or all strings. String sorts are case-insensitive in v1.44i or later.

This means the elements of the array need to be all strings or all numbers. All filenames in the macro language are strings, so any filename list can be sorted.

(Note, though, that the sort will be alphabetical, not numerical – a10.tif comes before a9.tif!)

I’m not sure what sort order you want in your array. But typically you want to work on the ch01 and the ch02 from the same image. One way to handle that is to use the filename loop to open the ch01 image, and then, in the processing loop, open the ch02 image with the same base name.

Some of the batch processing templates include code to process filenames containing a specified string – you can use that as a basis to filter on the “ch01” files.

Hope this helps.

1 Like