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.
It is dependent on the algorithm, but the default Julia sort algorithm is stable, which means that equal keys will be in the same order after sort as before. You can supply a function to sort that gives the relative order of two elements. See the lt keyword argument.
Those aren’t tuples, they’re vectors, and they already sort lexicographically. Thus, we can generically have by return the list of keys in order to compare: