zjwang zjwang - 1 year ago 73
Ruby Question

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

I have a ruby array in the format of


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?


Answer Source

You can easily do it with

sort_by{ |o| [o.name, -o.score]}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download