Skip to content

Instantly share code, notes, and snippets.

@ggarnier
Created November 21, 2013 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggarnier/7586500 to your computer and use it in GitHub Desktop.
Save ggarnier/7586500 to your computer and use it in GitHub Desktop.
Ordering an array in Ruby using 2 sorting criterias
# 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