How to sort a column of cell arrays, using the same criteria as my table?

I have a table T1 with 1000 rows and 20 columns. I also have a column based cell array B with the size of 1000 x 1.

Here is my code to sort the table:
T1 = T1[sortperm(T1[:, 12]; rev=true), :]; # Sort by the depth values
T1 = T1[sortperm(T1[:, 2]), :]; # Sort by Station ID
T1 = T1[sortperm(T1[:, 1]), :]; # Sort by Expedition IDs

How do I apply the same ordering index to B?

Thanks.

Simplest is to save the result of sortperm and use it to index B.

p = sortperm(@view T1[:, 12]; rev=true)
T1 = T1[p, :]
B = B[p]
1 Like

Many thanks.

Do you know how to get the final sort index based on 3 sorting practices?

Another method:

p = sortperm(eachrow(T1); by=r->(r[1], r[2], -r[12]))
T1 = T1[p, :]
B = B[p, :]

(didn’t really test this, but it should work)

1 Like

Is this what you want? It should give a primary sort by Expedition ID, then by Station ID, then by reverse depth.

p = sortperm(eachrow(T1), by=r->(r[1],r[2],-r[12]))

(Too slow, I got scooped by Dan)

2 Likes

Thank you so much for your help.

Thank you for the solution, Dan!