Last active
January 2, 2016 00:49
-
-
Save aarti/8226579 to your computer and use it in GitHub Desktop.
Sort hash simply and functionally
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
db = { 7 => ["parikh", "shah"], 5 => ["sinha", "kaur"], 3 => ["sharma", "khan"]} | |
def sort_functional(db) | |
db.keys.sort.reduce({}) { |hash,item| hash.merge( item => db[item].sort ) } | |
end | |
def sort_simple(db) | |
sorted = {} | |
db.keys.sort.each do |n| | |
sorted[n]=db[n].sort | |
end | |
sorted | |
end | |
print sort_simple db | |
print "\n" | |
print sort_functional db |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment