Pierre Michard Pierre Michard - 4 years ago 107
Ruby Question

What is the most elegant way to sort ranges in ruby

I need to sort a table of objects of type

by their start point. For that I have the following code which works fine:

ranges = @ranges.sort do |a,b|
(a.min) <=> (b.min)

I was just wondering if there was a shorter and elegant way to do the same thing.

Answer Source

How about:

ranges = @ranges.sort_by(&:min)


ranges = @ranges.sort_by(&:first)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download