# Sorting multidimensional arrays

**URL:** <https://discourse.julialang.org/t/sorting-multidimensional-arrays/71657>\
**Category:** Performance\
**Tags:** question, sort, arrays\
**Created:** [November 17, 2021, 12:56pm UTC](https://discourse.julialang.org/t/sorting-multidimensional-arrays/71657 "2021-11-17T12:56:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rolim.gustavo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rolim.gustavo/32/46941_2.png) [@rolim.gustavo](https://discourse.julialang.org/u/rolim.gustavo)\
**Post date:** [November 17, 2021, 12:56pm UTC](https://discourse.julialang.org/t/sorting-multidimensional-arrays/71657/1 "2021-11-17T12:56:56Z")

</div>

Hi,

I am currently working on a scheduling problem which has a solution represented by a multidimensional array called Sol. For instance, Sol = [[4, 1, 5, 2], [3, 6, 10, 9, 7, 8]]. Each row is represented by a permutation that may have different lenghts. I have also an ordered array with the same number of rows that is called OF such that OF = [448, 341]. I apply sortperm(OF) to find the order that I should sort Sol. For example, sortpem(OF) = [2, 1]. In this case, I would have to sort Sol such that Sol = [[3, 6, 10, 9, 7, 8], [4, 1, 5, 2]]. I would like to know if there is an efficient way to sort the the rows, which are individual arrays in Sol.

If this post is in the wrong category, please feel free to move it appropriately.

Thanks  
Gustavo

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 17, 2021, 1:15pm UTC](https://discourse.julialang.org/t/sorting-multidimensional-arrays/71657/2 "2021-11-17T13:15:32Z")

</div>

Not clear. With

```julia
order=sortperm(OF)

```

you already have `Sol` in the order you want it to be:

```julia
julia> Sol[order]
2-element Vector{Vector{Int64}}:
 [3, 6, 10, 9, 7, 8]
 [4, 1, 5, 2]

```

What do you mean with

> I would have to sort Sol such that…

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 17, 2021, 1:37pm UTC](https://discourse.julialang.org/t/sorting-multidimensional-arrays/71657/3 "2021-11-17T13:37:14Z")

</div>

> [@rolim.gustavo](#):
>
> I am currently working on a scheduling problem which has a solution represented by a multidimensional array called Sol. For instance, Sol = [[4, 1, 5, 2], [3, 6, 10, 9, 7, 8]].

That’s not a multidimensional array. It’s a 1d array of 1d arrays. (Unlike Python, Julia has a distinct native type for multidimensional arrays, stored contiguously in memory.)

> [@rolim.gustavo](#):
>
> I would like to know if there is an efficient way to sort the the rows, which are individual arrays in Sol.

`sort!.(Sol)` will sort each of the “rows” in-place.

---

<div class="post-metadata">

**Author:** ![rolim.gustavo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rolim.gustavo/32/46941_2.png) [@rolim.gustavo](https://discourse.julialang.org/u/rolim.gustavo)\
**Post date:** [November 17, 2021, 3:45pm UTC](https://discourse.julialang.org/t/sorting-multidimensional-arrays/71657/4 "2021-11-17T15:45:48Z")

</div>

Thanks for your comment oheil. I will try to give a clearer explanation. Each element of the array Sol is another array containing a permutation. The array OF has the same length of Sol, which contains a value associated with each permutation in Sol. In the example above, the element Sol[1] is [4, 1, 5, 2] which has an associated value of OF[1] that is equal to 448. Analogously, the element Sol[2] is [3, 6, 10, 9, 7, 8], which has the associated value of OF[2] that is equal to 341. First, I use order = sortperm(OF) to find the order of the indices that I need to sort Sol. In this case, Julia would return [2, 1] which is the order that I want Sol to be. I tried Sol[order] and it worked well.
