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

Naturally

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.

Installation

Add this line to your application's Gemfile:

gem 'naturally'

And then execute:

$ bundle

Or install it yourself as:

$ gem install naturally

Usage

# 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 = Struct.new(:number, :name)
objects = [
  Thing.new('1.1', 'color'),
  Thing.new('1.2', 'size'),
  Thing.new('1.1.1', 'opacity'),
  Thing.new('1.1.2', 'lightness'),
  Thing.new('1.10', 'hardness'),
  Thing.new('2.1', 'weight'),
  Thing.new('1.3', 'shape')
  ]
sorted = objects.sort_by{ |o| Naturally.normalize(o.number) }

See the spec for more examples.

Contributing

  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.