Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (39 sloc) 1.47 KB


Natural sorting with support for legal document numbering. See Counting to 10 in Californian for the motivations to make this library.

I got most of this code from an open source project, but I forget which. (!) I then made a few changes to handle the particular types of numbers that come up in statutes, such as 335.1, 336, 336a, etc.


Add this line to your application's Gemfile:

gem 'naturally'

And then execute:

$ bundle

Or install it yourself as:

$ gem install naturally


# Sort a simple array of strings
Naturally.sort(["1.1", "1.10", "1.2"])  # => ["1.1", "1.2", "1.10"]

# Sort an array of objects by one attribute
objects.sort_by{ |o| Naturally.normalize(o.number) }
Thing =, :name)
objects = ['1.1', 'color'),'1.2', 'size'),'1.1.1', 'opacity'),'1.1.2', 'lightness'),'1.10', 'hardness'),'2.1', 'weight'),'1.3', 'shape')
sorted = objects.sort_by{ |o| Naturally.normalize(o.number) }

See the spec for more examples.


  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
Jump to Line
Something went wrong with that request. Please try again.