Reordering Rows in the Results Table


I am analyzing some threshold-segmented areas of interest using a binary mask that is applied to my test image using Analyze Particles. The output that I have selected is for a labeled map that corresponds to the numbered row of my results table.

I wanted to check to see if there was a way to reorder the results table so that my areas of interest are ordered in the table as they appear left to right on the map without me having to copy the table and reorder it myself in a spreadsheet.

For example, I’d like my results table to be ordered as 7, 6, 5, 4, 3, 2, 1 going down the list for this particular set.

-Mary Beth

Screenshot 2021-01-13 122034

Not sure if you really want to invert tables according to the row numbers or sort tables according to the Area sizes …

In case of the latter, here is some IJ-macro code that does just this:

// make a test table with your Area-data
a = newArray(29036,29631,27073,23464,19336,13688,13479);
Table.create("Mary Beth");
Table.showRowNumbers( true );
Table.setColumn( "Area", a );
// end

// sort any table according to the Area data (from small to big)
// end


Thanks for the quick response! Actually, I am trying to use the row numbers instead of the areas for this ordering. Not sure if it’s possible to do that based on their appearance from left to right in the image?

Mary Beth

There may be a more elegant solution but the following macro code inverts the table rows:

n = Table.size;
b = newArray(n);
for ( i=1; i<n+1; i++ ) b[i-1]=n+1-i;
Table.setColumn("R#", b);

Please accept that the above code creates an additional column that you may delete in case it bothers.

1 Like

Thanks again! I think this is getting closer, but I have other data sets where the rows are not just ordered in descending order.

For example, left to right across the image the ROIs may be labeled: 3,4,7,1,2,6,5 while the results table is ordered numerically and must be resorted in Excel to the left to right order that I would want.

What you need is a column that contains X-coordinates.
You can obtain this information by including “Centroid” to your measurements.

To sort tables then use:


Got it! Thank you!

Mary Beth

The reason for this is that the Analyze Particles command in ImageJ labels your regions in the order of the y start coordinate (i.e., it’s scanning the image for objects row by row, and objects at the top of the image will be encountered first).

If your objects are always arranged in a row from left to right, an easy way to get the results in the correct order would be to rotate your image (e.g. Image › Transform › Rotate 90 Degrees Right) before doing the measurement.