Naturally
Natural (version number) sorting with support for legal document numbering, college course codes, and Unicode. See Jeff Atwood's Sorting for Humans: Natural Sort Order and the WebLaws.org post Counting to 10 in Californian.
Usage
require 'naturally'
# Sort a simple array of strings
Naturally.sort(["1.1", "1.10", "1.2"]) # => ["1.1", "1.2", "1.10"]
Usually the library is used to sort an array of objects:
# Define a new simple object for storing Ubuntu versions
UbuntuVersion = Struct.new(:name, :version)
# Create an array
releases = [
UbuntuVersion.new('Saucy Salamander', '13.10'),
UbuntuVersion.new('Raring Ringtail', '13.04'),
UbuntuVersion.new('Precise Pangolin', '12.04.4'),
UbuntuVersion.new('Maverick Meerkat', '10.10'),
UbuntuVersion.new('Quantal Quetzal', '12.10'),
UbuntuVersion.new('Lucid Lynx', '10.04.4')
]
# Sort by version number
sorted = Naturally.sort_by(releases, :version)
# Check what we have
expect(sorted.map(&:name)).to eq [
'Lucid Lynx',
'Maverick Meerkat',
'Precise Pangolin',
'Quantal Quetzal',
'Raring Ringtail',
'Saucy Salamander'
]
See the spec for more examples of what Naturally can sort.
Related Work
- ahoward/version_sorter, the starting point for the
naturally
gem. - GitHub's Version sorter
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request