Skip to content

Instantly share code, notes, and snippets.

@linx4200
Last active August 7, 2016 15:50
Show Gist options
  • Save linx4200/d34ceca68812aec40df14365701427dc to your computer and use it in GitHub Desktop.
Save linx4200/d34ceca68812aec40df14365701427dc to your computer and use it in GitHub Desktop.
根据多个键值来对数组排序
def sort_by_multiple_keys(arr)
# <=> operator to compare each element of the collection.
# This operator takes two arguments
# returns
# -1 if the first argument is less than the second argument
# 1 if the second argument is less than the first argument
# 0 if they're equivalent.
# 1 <=> 2 # => -1
# 2 <=> 1 # => 1
# 1 <=> 1 # => 0
arr.sort { |a, b|
[b[:key1], b[:key2], b[:key3]] <=> [a[:key1], a[:key2], a[:key3]]
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment