Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 332
Wali Waqar Avatar asked Mar 19 '26 11:03

Wali Waqar


1 Answers

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
like image 157
Bogumił Kamiński Avatar answered Mar 21 '26 12:03

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!