zjwang zjwang - 3 months ago 5
Ruby Question

How to apply a secondary sort for ruby array with reversed sort direction?

I have a ruby array in the format of

[["Anna",70],["Billy",90],["Billy",100]]
...

I know I can apply secondary sort using the following

sort_by{ |o| [o.name, o.score]}


However I want to sort first on Name asc and then sort on Score but descending. How could I achieve this?

Thanks

Answer

You can easily do it with

sort_by{ |o| [o.name, -o.score]}