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

**URL:** https://discourse.julialang.org/t/how-to-sort-a-column-of-cell-arrays-using-the-same-criteria-as-my-table/130278
**Category:** General Usage
**Tags:** question, sortperm
**Created:** [June 27, 2025, 3:57pm UTC](https://discourse.julialang.org/t/how-to-sort-a-column-of-cell-arrays-using-the-same-criteria-as-my-table/130278 "2025-06-27T15:57:04Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Leon6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leon6/32/205483_2.png) [@Leon6](https://discourse.julialang.org/u/Leon6)
#### Post date: [June 27, 2025, 3:57pm UTC](https://discourse.julialang.org/t/how-to-sort-a-column-of-cell-arrays-using-the-same-criteria-as-my-table/130278/1 "2025-06-27T15:57:04Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [June 27, 2025, 4:03pm UTC](https://discourse.julialang.org/t/how-to-sort-a-column-of-cell-arrays-using-the-same-criteria-as-my-table/130278/2 "2025-06-27T16:03:56Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![Leon6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leon6/32/205483_2.png) [@Leon6](https://discourse.julialang.org/u/Leon6)
#### Post date: [June 27, 2025, 4:08pm UTC](https://discourse.julialang.org/t/how-to-sort-a-column-of-cell-arrays-using-the-same-criteria-as-my-table/130278/3 "2025-06-27T16:08:03Z")

</div>

Many thanks.

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

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [June 27, 2025, 4:18pm UTC](https://discourse.julialang.org/t/how-to-sort-a-column-of-cell-arrays-using-the-same-criteria-as-my-table/130278/4 "2025-06-27T16:18:37Z")

</div>

Another method:

```julia
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)

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [June 27, 2025, 4:20pm UTC](https://discourse.julialang.org/t/how-to-sort-a-column-of-cell-arrays-using-the-same-criteria-as-my-table/130278/5 "2025-06-27T16:20:45Z")

</div>

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

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

```

(Too slow, I got scooped by Dan)

---

<div class="post-metadata">

### Author: ![Leon6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leon6/32/205483_2.png) [@Leon6](https://discourse.julialang.org/u/Leon6)
#### Post date: [June 27, 2025, 4:28pm UTC](https://discourse.julialang.org/t/how-to-sort-a-column-of-cell-arrays-using-the-same-criteria-as-my-table/130278/6 "2025-06-27T16:28:35Z")

</div>

> [@Jeff\_Emanuel](#):
>
> `= sortperm(@view T1[:, 12]; rev=true)`

Thank you so much for your help.

---

<div class="post-metadata">

### Author: ![Leon6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leon6/32/205483_2.png) [@Leon6](https://discourse.julialang.org/u/Leon6)
#### Post date: [June 27, 2025, 4:28pm UTC](https://discourse.julialang.org/t/how-to-sort-a-column-of-cell-arrays-using-the-same-criteria-as-my-table/130278/7 "2025-06-27T16:28:54Z")

</div>

Thank you for the solution, Dan!
