Skip to content

Instantly share code, notes, and snippets.

@faif
Last active December 18, 2015 12:58
Show Gist options
  • Save faif/5786178 to your computer and use it in GitHub Desktop.
Save faif/5786178 to your computer and use it in GitHub Desktop.
Ruby sort strings numerically native
irb(main):002:0> ['A3', 'A5', 'A11', 'A8', 'A10'].sort
=> ["A10", "A11", "A3", "A5", "A8"]
irb(main):003:0> ['A3', 'A5', 'A11', 'A8', 'A10'].sort_by { |x| x[1..x.size].to_i }
=> ["A3", "A5", "A8", "A10", "A11"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment