# Sort by index an array

**URL:** <https://discourse.julialang.org/t/sort-by-index-an-array/18608>\
**Category:** New to Julia\
**Tags:** sortperm\
**Created:** [December 12, 2018, 6:37pm UTC](https://discourse.julialang.org/t/sort-by-index-an-array/18608 "2018-12-12T18:37:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [December 12, 2018, 6:37pm UTC](https://discourse.julialang.org/t/sort-by-index-an-array/18608/1 "2018-12-12T18:37:48Z")

</div>

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

```julia
a = [0.4, 0.8, 0.2]

```

I would like to get a result like that:

```julia
order = [3,1,2]

```

so that

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

```

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [December 12, 2018, 6:48pm UTC](https://discourse.julialang.org/t/sort-by-index-an-array/18608/2 "2018-12-12T18:48:27Z")

</div>

`sortperm`.

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

```
