# Sortperm issue?

**URL:** <https://discourse.julialang.org/t/sortperm-issue/48351>\
**Category:** General Usage\
**Tags:** sortperm\
**Created:** [October 14, 2020, 9:03am UTC](https://discourse.julialang.org/t/sortperm-issue/48351 "2020-10-14T09:03:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [October 14, 2020, 9:03am UTC](https://discourse.julialang.org/t/sortperm-issue/48351/1 "2020-10-14T09:03:50Z")

</div>

I work with Julia 1.5.1 on Linux. I have this array

```julia
x = [0.3575631591952295;
        -1.2891687824408367;
        -0.5123400980902758;
        -1.61271953171407;
         1.0854706981323956;
         0.2748568949784879;
         1.6127195317140701;
         0.021835136639906272;
         0.27038905630106824;
        -0.20860606471597504]

```

When I do `sortperm` I expect to get

```julia
8
2
3
1
9
7
10
5
6
4

```

but I get

```julia
sortperm(x)
10-element Array{Int64,1}:
  4
  2
  3
 10
  8
  9
  6
  1
  5
  7

```

which makes no sense.

Note however that `sort()` do work correctly

```julia
sort(x)
10-element Array{Float64,1}:
 -1.61271953171407
 -1.2891687824408367
 -0.5123400980902758
 -0.20860606471597504
  0.021835136639906272
  0.27038905630106824
  0.2748568949784879
  0.3575631591952295
  1.0854706981323956
  1.6127195317140701

```

and on some other examples `sortperm` work.

Am I doing something wrong? Is there something I don’t see? Can someone reproduce that error ?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 14, 2020, 9:13am UTC](https://discourse.julialang.org/t/sortperm-issue/48351/2 "2020-10-14T09:13:37Z")

</div>

It seems correct to me:

```julia
julia> x = [0.3575631591952295;
               -1.2891687824408367;
               -0.5123400980902758;
               -1.61271953171407;
                1.0854706981323956;
                0.2748568949784879;
                1.6127195317140701;
                0.021835136639906272;
                0.27038905630106824;
               -0.20860606471597504];

julia> ind = sortperm(x)
10-element Array{Int64,1}:
  4
  2
  3
 10
  8
  9
  6
  1
  5
  7

julia> x[ind]
10-element Array{Float64,1}:
 -1.61271953171407
 -1.2891687824408367
 -0.5123400980902758
 -0.20860606471597504
  0.021835136639906272
  0.27038905630106824
  0.2748568949784879
  0.3575631591952295
  1.0854706981323956
  1.6127195317140701

julia> x[ind] == sort(x)
true

```

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [October 14, 2020, 9:25am UTC](https://discourse.julialang.org/t/sortperm-issue/48351/3 "2020-10-14T09:25:04Z")

</div>

My bad I got confused with the rank! Which would be

```julia
using Random
ordinalrank(x)
10-element Array{Int64,1}:
  8
  2
  3
  1
  9
  7
 10
  5
  6
  4

```
