Ruby `sort_by` Multiple Fields
Ruby’s sort_by
is handy, but I needed to sort by multiple parameters, and thought I would have to write my own <=>
method on a custom class. But in fact you can do this:
array.sort_by do |item|
[item.a, item.b]
end
Not documented, but works great.
Written by Max Howell
Related protips
3 Responses
interesting
over 1 year ago
·
you can see this doc http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-sort_by
over 1 year ago
·
There is nothing to document here, it`s just a consequence of Array having <=> defined on it )
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#