Skip to content

Instantly share code, notes, and snippets.

@jin
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jin/3e5dbf787dbf41f0c5d6 to your computer and use it in GitHub Desktop.
Save jin/3e5dbf787dbf41f0c5d6 to your computer and use it in GitHub Desktop.
Custom sort comparators in Ruby
def sort hash
hash.keys.map(&:to_s).sort(& -> (a, b) { a.length <=> b.length })
end
p sort ({ abc: 'hello', 'another_key' => 123, 4567 => 'third' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment