Skip to content

Instantly share code, notes, and snippets.

@sigmonsays
Created September 26, 2009 01:29
Show Gist options
  • Save sigmonsays/193969 to your computer and use it in GitHub Desktop.
Save sigmonsays/193969 to your computer and use it in GitHub Desktop.
sort_func = Proc.new { |a,b|
b[1] <=> a[1]
}
h = {
'foo' => 5,
'bar' => 50,
'baz' => 500,
}
# i want something like this.. where sort_func can be passed in like an argument:
# sorted = h.sort sort_func
# sorted.each { |k,v|
# puts "#{k} #{v}"
# }
# This can also be written as
h.sort { |a,b| b[1] <=> a[1] }.each { |k,v|
puts "#{k} #{v}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment