Sort by index an array

Which Julia’s function returns the indexes that would sort an array?

a = [0.4, 0.8, 0.2]

I would like to get a result like that:

order = [3,1,2]

so that

a[order]
3-element Array{Float64,1}:
 0.2
 0.4
 0.8
2 Likes

sortperm.

help?> sortperm

  sortperm(v; alg::Algorithm=DEFAULT_UNSTABLE, lt=isless, by=identity, rev::Bool=false, order::Ordering=Forward)

  Return a permutation vector I that puts v[I] in sorted order. The order is
  specified using the same keywords as sort!. The permutation is guaranteed to
  be stable even if the sorting algorithm is unstable, meaning that indices of
  equal elements appear in ascending order.
4 Likes