# Built in function to sort the results of index in

**URL:** https://discourse.julialang.org/t/built-in-function-to-sort-the-results-of-index-in/60624
**Category:** General Usage
**Tags:** array
**Created:** [May 6, 2021, 7:21am UTC](https://discourse.julialang.org/t/built-in-function-to-sort-the-results-of-index-in/60624 "2021-05-06T07:21:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)
#### Post date: [May 6, 2021, 7:21am UTC](https://discourse.julialang.org/t/built-in-function-to-sort-the-results-of-index-in/60624/1 "2021-05-06T07:21:50Z")

</div>

` x =[2 3 4 5 6 7 8] z = [2 6 5] y= indexin(z,x)`

is there a built in function to sort the results of indexin

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [May 6, 2021, 9:06am UTC](https://discourse.julialang.org/t/built-in-function-to-sort-the-results-of-index-in/60624/2 "2021-05-06T09:06:10Z")

</div>

The problem here is that you start with (1,n) matrices x and z:

```julia
julia> x =[2 3 4 5 6 7 8]
1×7 Matrix{Int64}:
 2 3 4 5 6 7 8

```

which results in the end, that `indexin` returns 2-dimensional indices:

```julia
julia> y[2]
CartesianIndex(1, 5)

```

where order isn’t defined per default so you can’t use `sort!` on `y`.  
If `indexin` would provide a “return sorted indices” parameter, the problem would be same that no order is defined.

So the question is: is it mandatory, that you are dealing with

```julia
1×7 Matrix{Int64}

```

? Or tis this just some oversight? If you actually have vectors, it would be straight forward:

```julia
julia> z = [2, 6, 5]
3-element Vector{Int64}:
 2
 6
 5

julia> y= indexin(z,x)
3-element Vector{Union{Nothing, Int64}}:
 1
 5
 4

julia> y= sort(indexin(z,x))
3-element Vector{Union{Nothing, Int64}}:
 1
 4
 5

```

You can reshape your matrices with:

```julia
julia> x =[2 3 4 5 6 7 8]
1×7 Matrix{Int64}:
 2 3 4 5 6 7 8

julia> x = reshape([2 3 4 5 6 7 8],length(x))
7-element Vector{Int64}:
 2
 3
 4
 5
 6
 7
 8

```

---

<div class="post-metadata">

### Author: ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)
#### Post date: [May 6, 2021, 11:39am UTC](https://discourse.julialang.org/t/built-in-function-to-sort-the-results-of-index-in/60624/3 "2021-05-06T11:39:11Z")

</div>

thanks!!! worked
