# How to order a array of tuples

**URL:** <https://discourse.julialang.org/t/how-to-order-a-array-of-tuples/26000>\
**Category:** New to Julia\
**Created:** [July 4, 2019, 2:40am UTC](https://discourse.julialang.org/t/how-to-order-a-array-of-tuples/26000 "2019-07-04T02:40:57Z")\
**Posts on this page:** 5\
**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:** [July 4, 2019, 2:40am UTC](https://discourse.julialang.org/t/how-to-order-a-array-of-tuples/26000/1 "2019-07-04T02:40:57Z")

</div>

I have array of tuples and I want to ordered these tuples.

`[(2, 3.1), (1, 5.5), (3, 4.5)]`

for example, tuples can be ordered decreasing according to their first element.

```julia
[(3, 4.5),
 (2, 3.1),
 (1, 5.5)]

```

Is there a simple way to do this?

---

<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:** [July 4, 2019, 3:16am UTC](https://discourse.julialang.org/t/how-to-order-a-array-of-tuples/26000/2 "2019-07-04T03:16:52Z")

</div>

You can simply run `sort()` on a vector of tuples, and if you want a decreasing order the you can pass `rev=true` :

```julia
julia> t = [(2, 3.1), (1, 5.5), (3, 4.5)]
3-element Array{Tuple{Int64,Float64},1}:
 (2, 3.1)
 (1, 5.5)
 (3, 4.5)

julia> sort(t, rev=true)
3-element Array{Tuple{Int64,Float64},1}:
 (3, 4.5)
 (2, 3.1)
 (1, 5.5)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 4, 2019, 7:19am UTC](https://discourse.julialang.org/t/how-to-order-a-array-of-tuples/26000/3 "2019-07-04T07:19:59Z")

</div>

Alternatively to @rdeits’s solution, you can do

```julia
julia> sort([(2, 3.1), (1, 5.5), (3, 4.5)], by = first, rev = true)
3-element Array{Tuple{Int64,Float64},1}:
 (3, 4.5)
 (2, 3.1)
 (1, 5.5)

```

which will only consider the _first_ element.

---

<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:** [July 4, 2019, 2:06pm UTC](https://discourse.julialang.org/t/how-to-order-a-array-of-tuples/26000/4 "2019-07-04T14:06:45Z")

</div>

@Tamas_Papp, @rdeits Thank you so much for your help.

sort works only for the first element?

A further example would be to order this array:

`t = [(3, 2), (2, 2), (2, 1), (1, 3), (1, 2), (1, 1)]`

And I would like to sort it so that the first elements remain in the same order i.e. 3, 2, 2, 1, 1, 1, but the second elements are ordered the opposite way so it’s like this:

```julia
[(3, 2), (2, 1), (2, 2), (1, 1), (1, 2), (1, 3)]

```

i.e. the tuples are ordered such that the first elements are in decreasing order and where the first element is the same the tuples are ordered with second elements in increasing order.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 4, 2019, 2:31pm UTC](https://discourse.julialang.org/t/how-to-order-a-array-of-tuples/26000/5 "2019-07-04T14:31:10Z")

</div>

> [@Elmer\_Cusipuma](#):
>
> sort works only for the first element?

No, it works by comparing elements with `isless` by default. `isless` is defined lexicographically for tuples. You can reverse that (as @rdeits suggested), or just explicitly compare the first element (as I suggested).

> [@Elmer\_Cusipuma](#):
>
> sort it so that the first elements remain in the same order i.e. 3, 2, 2, 1, 1, 1, but the second elements are ordered the opposite way so it’s like this

define a custom comparison function, eg

```julia
sort(those_tuples,
     lt = (x, y) -> (x[1] > y[1] || (x[1] == y[1] && x[2] < y[2]))

```
