Skip to content

Instantly share code, notes, and snippets.

@404pnf
Last active December 28, 2015 20:48
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 404pnf/7559645 to your computer and use it in GitHub Desktop.
Save 404pnf/7559645 to your computer and use it in GitHub Desktop.
naive implementation of alphanumeric sort in ruby
>> 'a11'.split('').map { |e| e[/\d/] ? e : e.ord }.join
=> "9711"
>> def conv str
>> str.split('').map { |e| e[/\d/] ? e : e.ord }.join
>> end
=> nil
>> a
=> ["a", "a1", "a12", "b", "a2"]
>> a.sort_by { |e| conv e}
=> ["a", "a1", "a12", "a2", "b"]
@404pnf
Copy link
Author

404pnf commented Nov 20, 2013

doesn't work as expected

%w(1.1 1.11 1.2).sort_by { |e| conv e}
=> ["1.1", "1.11", "1.2"] # wrong

%w(1.1 1.1.1 1.2).sort_by { |e| conv e}
=> ["1.1", "1.1.1", "1.2"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment