# Question regarding Sortrows usage with anonymous function

**URL:** https://discourse.julialang.org/t/question-regarding-sortrows-usage-with-anonymous-function/6678
**Category:** New to Julia
**Tags:** sort
**Created:** [October 25, 2017, 1:45pm UTC](https://discourse.julialang.org/t/question-regarding-sortrows-usage-with-anonymous-function/6678 "2017-10-25T13:45:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)
#### Post date: [October 25, 2017, 1:45pm UTC](https://discourse.julialang.org/t/question-regarding-sortrows-usage-with-anonymous-function/6678/1 "2017-10-25T13:45:18Z")

</div>

Hi,

I am trying to understand the command sortrows using the option “by”.

If I have a 3x2 array v:

> v = [1 2; 2 4; 3 1]

If I want to sort it by the second column, then I have to do:

`u=sortrows(v, by=x -> x[2])`

I understand that the construction x → x is an anonymous function.

However, why the construction `x -> x[2]` means that it should sort array v by the 2nd column ? Can someone provide a little bit more of detail about it ?

thank you

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [October 25, 2017, 1:55pm UTC](https://discourse.julialang.org/t/question-regarding-sortrows-usage-with-anonymous-function/6678/2 "2017-10-25T13:55:27Z")

</div>

Checking the docs for `sortrows!` tells you to check the docs for `sort!` to find out about keyword arguments:

```julia
help?> sort!
...

The by keyword lets you provide a function that will be applied to each element before comparison;
...                                             

```

So as the elements are rows `x -> x[2]` will return the entry in the second column of the row currently being sorted. And that will be used for comparison.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [October 25, 2017, 1:55pm UTC](https://discourse.julialang.org/t/question-regarding-sortrows-usage-with-anonymous-function/6678/3 "2017-10-25T13:55:44Z")

</div>

Does this help?

```julia
julia> function by(x)
           println("checking: $x and extracting $(x[2])")
           x[2]
       end
by (generic function with 1 method)

julia> v = [1 2; 2 4; 3 1]
3×2 Array{Int64,2}:
 1 2
 2 4
 3 1

julia> sortrows(v, by=by)
checking: [2, 4] and extracting 4
checking: [1, 2] and extracting 2
checking: [1, 2] and extracting 2
checking: [2, 4] and extracting 4
checking: [3, 1] and extracting 1
checking: [2, 4] and extracting 4
checking: [2, 4] and extracting 4
checking: [3, 1] and extracting 1
checking: [3, 1] and extracting 1
checking: [1, 2] and extracting 2
checking: [1, 2] and extracting 2
checking: [3, 1] and extracting 1
3×2 Array{Int64,2}:
 3 1
 1 2
 2 4

```

---

<div class="post-metadata">

### Author: ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)
#### Post date: [October 25, 2017, 2:12pm UTC](https://discourse.julialang.org/t/question-regarding-sortrows-usage-with-anonymous-function/6678/4 "2017-10-25T14:12:20Z")

</div>

thanks, it is very clear now.
