Skip to content
Natural sort algorithm
Branch: master
Clone or download
dogweather New Syntax (#26)
* chore: failing test

* docs: New syntax in the README

* refactor: coding style

* feature: tests pass - new syntax
Latest commit ef29888 Jun 3, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE.txt initial commit Dec 8, 2012 New Syntax (#26) Jun 3, 2018


Gem Version Build Status Maintainability

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 Public.Law post Counting to 10 in Californian.


$ gem install naturally


require 'naturally'

# Sort a simple array of strings
Naturally.sort(["336", "335a", "335", "335.1"])  # => ["335", "335.1", "335a", "336"]

Usually the library is used to sort an array of objects:

# Define a new simple object for storing Ubuntu versions
UbuntuVersion =, :version)

# Create an array
releases = ['Saucy Salamander', '13.10'),'Raring Ringtail',  '13.04'),'Precise Pangolin', '12.04.4'),'Maverick Meerkat', '10.10'),'Quantal Quetzal',  '12.10'),'Lucid Lynx',       '10.04.4')

# Sort by version number
sorted = Naturally.sort releases, by: :version

# Check what we have
expect( eq [
  'Lucid Lynx',
  'Maverick Meerkat',
  'Precise Pangolin',
  'Quantal Quetzal',
  'Raring Ringtail',
  'Saucy Salamander'

More examples are in the specs.

Implementation Notes

The algorithm capitalizes on Ruby's array comparison behavior: Since each dotted number actually represents a hierarchical identifier, array comparison is a natural fit:

Arrays are compared in an “element-wise” manner; the first element of ary is compared with the first one of other_ary using the <=> operator, then each of the second elements, etc… As soon as the result of any such comparison is non zero (i.e. the two corresponding elements are not equal), that result is returned for the whole array comparison.

And so, when given input such as,

['1.9', '1.9a', '1.10']

...this module sorts the segmented numbers by comparing them in their array forms:

[['1', '9'], ['1', '9a'], ['1', '10']]

Finally, upon actual sort comparison, each of these strings is converted to an array of typed objects. This is to determine the sort order between heterogenous (yet ordered) segments such as '9a' and '9'.

The final nested comparison structure looks like this:

     [1], [9]
     [1], [9, 'a']
     [1], [10]

Related Work


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
You can’t perform that action at this time.