# Sort an array according other array

**URL:** https://discourse.julialang.org/t/sort-an-array-according-other-array/68295
**Category:** General Usage
**Tags:** question, sort, sortperm, arrays
**Created:** [September 17, 2021, 1:27am UTC](https://discourse.julialang.org/t/sort-an-array-according-other-array/68295 "2021-09-17T01:27:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)
#### Post date: [September 17, 2021, 1:27am UTC](https://discourse.julialang.org/t/sort-an-array-according-other-array/68295/1 "2021-09-17T01:27:37Z")

</div>

I am trying to sort an array according other array. More specifically, sort ε\_ according Z.

> NN = 6  
> Z = rand(NN)  
> ε\_= rand(NN)  
> teste = [Z[sortperm(Z)] ε\_[sortperm(ε\_)] ]  
> test\_f(Z) = teste[teste[:,1] .== Z , 2]  
> ε\_ = test\_f.(Z)

I believe there is a solution in fewer lines.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [September 17, 2021, 2:03am UTC](https://discourse.julialang.org/t/sort-an-array-according-other-array/68295/2 "2021-09-17T02:03:43Z")

</div>

`first.(sort(collect(zip(ε_,Z)),by=last))`

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [September 17, 2021, 2:16am UTC](https://discourse.julialang.org/t/sort-an-array-according-other-array/68295/3 "2021-09-17T02:16:51Z")

</div>

You mean?

`ε_ .= ε_[sortperm(Z)]`

---

<div class="post-metadata">

### Author: ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)
#### Post date: [September 17, 2021, 2:26am UTC](https://discourse.julialang.org/t/sort-an-array-according-other-array/68295/4 "2021-09-17T02:26:24Z")

</div>

No. It is the same that

> ε\_[sortperm(Z) , :]

But my goal is different. You can run my script and will see that it is not the same goal.

---

<div class="post-metadata">

### Author: ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)
#### Post date: [September 17, 2021, 2:26am UTC](https://discourse.julialang.org/t/sort-an-array-according-other-array/68295/5 "2021-09-17T02:26:43Z")

</div>

No. my goal is different. You can run my script and will see that it is not the same goal.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [September 17, 2021, 2:38am UTC](https://discourse.julialang.org/t/sort-an-array-according-other-array/68295/6 "2021-09-17T02:38:49Z")

</div>

Picking easier to read random things, I think this is a less convoluted way to get the same answer:

```julia
julia> Z = rand(Int8, 6); println(Z)
Int8[68, -64, -110, 95, -116, 81]

julia> ε_ = rand('a':'z', 6); println(ε_)
['i', 'b', 'u', 'k', 'v', 'j']

julia> teste = [Z[sortperm(Z)] ε_[sortperm(ε_)] ]
6×2 Matrix{Any}:
 -116 'b'
 -110 'i'
  -64 'j'
   68 'k'
   81 'u'
   95 'v'

julia> test_f(Z) = teste[teste[:,1] .== Z , 2]
test_f (generic function with 1 method)

julia> test_f.(Z) # desired result?
6-element Vector{Vector{Any}}:
 ['k']
 ['j']
 ['i']
 ['v']
 ['b']
 ['u']

julia> ε_[sortperm(Z)] |> println # "sort an array according other array"
['v', 'u', 'b', 'i', 'j', 'k']

julia> sort(ε_)[invperm(sortperm(Z))] |> println # more direct way?
['k', 'j', 'i', 'v', 'b', 'u']

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [September 17, 2021, 10:13am UTC](https://discourse.julialang.org/t/sort-an-array-according-other-array/68295/7 "2021-09-17T10:13:06Z")

</div>

Similar result but different output array type, just in case:

```julia
test_f2(Z) = teste[indexin(Z,teste[:,1]),2]
test_f2(Z)

6-element Vector{Any}:
 'k': ASCII/Unicode U+006B (category Ll: Letter, lowercase)
 'j': ASCII/Unicode U+006A (category Ll: Letter, lowercase)
 'i': ASCII/Unicode U+0069 (category Ll: Letter, lowercase)
 'v': ASCII/Unicode U+0076 (category Ll: Letter, lowercase)
 'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
 'u': ASCII/Unicode U+0075 (category Ll: Letter, lowercase)

```
