Last active
August 7, 2016 15:50
-
-
Save linx4200/d34ceca68812aec40df14365701427dc to your computer and use it in GitHub Desktop.
根据多个键值来对数组排序
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
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