Skip to content

Instantly share code, notes, and snippets.

@aarti
Last active January 2, 2016 00:49
Show Gist options
  • Save aarti/8226579 to your computer and use it in GitHub Desktop.
Save aarti/8226579 to your computer and use it in GitHub Desktop.
Sort hash simply and functionally
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