Question about sort?

Dear all,

how to get the index when using sort in Julia?

In Matlab ,we can get the index easily when using sort,

>> A = [7, 3, 6, 4];
>> [A, inds] = sort(A)

A =

     3     4     6     7


inds =

     2     4     3     1

In Julia, how to get the index?

julia> A = [7, 3, 6, 4];

julia> sort!(A)
4-element Vector{Int64}:
 3
 4
 6
 7

julia> sp = sortperm(A)
4-element Vector{Int64}:
 2
 4
 3
 1

julia> A[sp]
4-element Vector{Int64}:
 3
 4
 6
 7

Also, note that a lot of the time, if you want this, you really should be using a tree or priority queue or some more sophisticated data structure.

Thanks for your kind help.

1 Like