Sort a Ruby array with multiple criteria

Posted . Visible to the public.

If you want to sort a Ruby array with a primary, secondary, etc. criterium, use a sort_by that contains a block:

users.sort_by { |user| [user.age, user.name] }

This trick also works with our natural sort method.

Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2013-12-20 13:36)