Sorting multidimensional arrays

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

Not clear. With

order=sortperm(OF)

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

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…

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.)

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

1 Like

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.

1 Like