Permalink
Browse files

initial commit

  • Loading branch information...
dogweather committed Dec 8, 2012
1 parent a4f47c6 commit 7217e1e632270804b93e8b4b6796b3529c3b3164
Showing with 165 additions and 3 deletions.
  1. +4 −0 Gemfile
  2. +22 −0 LICENSE.txt
  3. +28 −3 README.md
  4. +1 −0 Rakefile
  5. +53 −0 lib/naturally.rb
  6. +3 −0 lib/naturally/version.rb
  7. +19 −0 naturally.gemspec
  8. +35 −0 spec/naturally_spec.rb
View
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in naturally.gemspec
+gemspec
View
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Robb Shecter
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -1,4 +1,29 @@
-naturally
-=========
+# Naturally
-Natural Sorting Ruby gem
+Natural sorting.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'naturally'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install naturally
+
+## Usage
+
+See the spec for 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
View
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
View
@@ -0,0 +1,53 @@
+require "naturally/version"
+
+module Naturally
+
+ # @return [Array] of strings sorted as if they
+ # were numbers.
+ def self.sort(an_array)
+ return an_array.sort_by{ |x| normalize(x) }
+ end
+
+
+ private
+
+ def self.normalize(version)
+ version.to_s.scan(%r/[0-9a-zA-Z]+/o).map{|i| NumberElement.new(i)}
+ end
+
+ class NumberElement
+ include Comparable
+ attr_accessor :val
+
+ def initialize(v)
+ @val = v
+ end
+
+ def <=>(other)
+ if pure_integer? && other.pure_integer?
+ return @val.to_i <=> other.val.to_i
+ elsif mixed? || other.mixed?
+ return simple_normalize(@val) <=> simple_normalize(other.val)
+ else
+ return @val <=> other.val
+ end
+ end
+
+ def pure_integer?
+ @val =~ /^\d+$/
+ end
+
+ def mixed?
+ val =~ /^\d+[a-zA-Z]+$/
+ end
+
+ def simple_normalize(n)
+ if n =~ /^(\d+)([a-zA-Z]+)$/
+ return [$1.to_i, $2]
+ else
+ return [n.to_i]
+ end
+ end
+ end
+
+end
View
@@ -0,0 +1,3 @@
+module Naturally
+ VERSION = "0.0.2"
+end
View
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'naturally/version'
+
+Gem::Specification.new do |gem|
+ gem.name = "naturally"
+ gem.version = Naturally::VERSION
+ gem.authors = ["Robb Shecter"]
+ gem.email = ["robb@weblaws.org"]
+ gem.description = %q{Sorts numbers in string form according to the way people are used to seeing them.}
+ gem.summary = %q{Natural Sorting}
+ gem.homepage = "http://github.com/dogweather/naturally"
+
+ gem.files = `git ls-files`.split($/)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.require_paths = ["lib"]
+end
View
@@ -0,0 +1,35 @@
+require 'naturally'
+
+describe Naturally do
+ describe '#sort' do
+ it 'sorts an array of strings nicely as if they were legal numbers' do
+ a = %w[676 676.1 676.11 676.12 676.2 676.3 676.9 676.10]
+ b = %w[676 676.1 676.2 676.3 676.9 676.10 676.11 676.12]
+ Naturally.sort(a).should == b
+ end
+
+ it 'sorts a smaller array of strings nicely as if they were legal numbers' do
+ a = %w[676 676.1 676.11 676.12 676.2 676.3 676.9]
+ b = %w[676 676.1 676.2 676.3 676.9 676.11 676.12]
+ Naturally.sort(a).should == b
+ end
+
+ it 'sorts a more complex list of strings' do
+ a = %w[350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.45 354.5]
+ b = %w[350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.5 354.45]
+ Naturally.sort(a).should == b
+ end
+
+ it 'sorts when numbers have letters in them' do
+ a = %w[335 335.1 336a 336 337 337a 337.1 337.15 337.2]
+ b = %w[335 335.1 336 336a 337 337.1 337.2 337.15 337a]
+ Naturally.sort(a).should == b
+ end
+
+ it 'sorts double digits with letters correctly' do
+ a = %w[12a 12b 12c 13a 13b 2 3 4 5 10 11 12]
+ b = %w[2 3 4 5 10 11 12 12a 12b 12c 13a 13b]
+ Naturally.sort(a).should == b
+ end
+ end
+end

0 comments on commit 7217e1e

Please sign in to comment.