Created
November 21, 2013 18:03
-
-
Save ggarnier/7586500 to your computer and use it in GitHub Desktop.
Ordering an array in Ruby using 2 sorting criterias
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ordering a list by field1 in descending order. If two elements have the same value, order by field2 in ascending order | |
list.sort do |a, b| | |
comp = (b.field1 <=> a.field2) | |
comp.zero? ? (a.field2 <=> b.field2) : comp | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment