Wali Waqar
Wali Waqar

Reputation: 385

Julia: Sort matrix by rows using custom comparator

I have the following two dimensional Array:

[120 320;
150 270;
230 250]

the rows of which I want to sort with regards to the second element in each row. I could not find anyway to do that using Julia's Base.sort(). Is it is possible to achieve this using Base.sort() or are there any alternatives?

Upvotes: 3

Views: 447

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69899

you can use sortslices for this:

julia> x = [120 320;
            150 270;
            230 250]
3×2 Array{Int64,2}:
 120  320
 150  270
 230  250

julia> sortslices(x, dims=1, by= x->x[2])
3×2 Array{Int64,2}:
 230  250
 150  270
 120  320

Upvotes: 7

Related Questions