Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Sorting by two values (basic sorting)

$
0
0

This is probably fairly simple. I want to sort a set of tuples first by the second value, and then by the first value, like this:

julia> a = [ rand(1:3,2) for i in 1: 5 ]
5-element Array{Array{Int64,1},1}:
 [2, 1]
 [3, 2]
 [3, 2]
 [1, 1]
 [2, 1]

julia> sort!(a, by = x -> x[2])
5-element Array{Array{Int64,1},1}:
 [2, 1]
 [1, 1]
 [2, 1]
 [3, 2]
 [3, 2]

julia> sort!(a, by = x -> x[1])
5-element Array{Array{Int64,1},1}:
 [1, 1]
 [2, 1]
 [2, 1]
 [3, 2]
 [3, 2]


The result is perfectly fine. However, I am somewhat unsure if the result of the second sorting might be dependent on the sorting algorithm used. Am I safe with that?

In this case I really don’t care about performance, but I wonder if there is a special function or syntax for this kind of thing.

8 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images