Skip to content

Commit

Permalink
docu and api
Browse files Browse the repository at this point in the history
  • Loading branch information
reiz committed Apr 4, 2012
1 parent 45cbde9 commit 216ed3e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
34 changes: 29 additions & 5 deletions README.markdown
Expand Up @@ -16,19 +16,36 @@ Because the default sort method does not recognize the numbers in the string. Th

## API

This GEM has just 2 methods
This GEM has 8 methods

`Naturalsorter::Sorter.sort(array, caseinsesitive)`
`Naturalsorter::Sorter.sort_desc(array, caseinsesitive)`

And this here for more advanced sorting
And this here for more advanced sorting. Where you can put in a array of objects and the method which should called on every object for comparison.

`Naturalsorter::Sorter.sort_by_method(array, method, caseinsesitive)`
`Naturalsorter::Sorter.sort_by_method_desc(array, method, caseinsesitive)`

This methods are based on a different algo. spezially optimizied for sorting version strings.

`Naturalsorter::Sorter.sort_version(array)`
`Naturalsorter::Sorter.sort_version_desc(array)`

This here is again for an array with objects.

`Naturalsorter::Sorter.sort_version_by_method(array, method)`
`Naturalsorter::Sorter.sort_version_by_method_desc(array)`

Get newest.

`Naturalsorter::Sorter.get_newest_version(first, second)`


## Installation

You should add this line to your Gemfile

`gem 'naturalsorter', '0.1.0'`
`gem 'naturalsorter', '0.2.0'`

and run this command in your app root directory

Expand All @@ -46,9 +63,16 @@ If you have more advanced objects you want to sort, you should use the second me

`Naturalsorter::Sorter.sort_by_method(users, "firstname", true)`

that's it!
Isn't that awesome?

The next methods are based on my own implementation, optimized for sorting version strings like "1.1, 1.4, 1.10"

`Naturalsorter::Sorter.sort_version(["1.10", "1.1", "1.2"])`

will return the array ["1.1", "1.2", "1.10"]


## Alan Davies

This project uses internal the natcmp implementation from Alan Davies. All glorry to him for his awesome work.
The first 4 methods in this librarie are internal based on the natcmp implementation from Alan Davies. All glorry to him for his awesome work.

37 changes: 22 additions & 15 deletions lib/naturalsorter.rb
Expand Up @@ -40,9 +40,10 @@ def self.sort_desc(array, caseinsesitive)
if (array.nil? || array.empty?)
return nil
end
array.sort { |a, b| Natcmp.natcmp(a,b,caseinsesitive) }
array.sort { |a, b| Natcmp.natcmp(b,a,caseinsesitive) }
end


# 'Natural order' sort for an array of objects.
def self.sort_by_method(array, method, caseinsesitive)
if (array.nil? || array.empty?)
Expand All @@ -51,32 +52,38 @@ def self.sort_by_method(array, method, caseinsesitive)
array.sort { |a,b| Natcmp.natcmp(a.send(method),b.send(method),caseinsesitive) }
end

def self.sort_desc_by_method(array, method, caseinsesitive)
def self.sort_by_method_desc(array, method, caseinsesitive)
if (array.nil? || array.empty?)
return nil
end
array.sort { |a, b| Natcmp.natcmp(a.send(method),b.send(method),caseinsesitive) }
array.sort { |a, b| Natcmp.natcmp(b.send(method),a.send(method),caseinsesitive) }
end


def self.sort_version(array)
return nil if (array.nil? || array.empty?)
array.sort { |a,b| Versioncmp.compare( a, b ) }
end

def self.sort_version_desc(array)
return nil if (array.nil? || array.empty?)
array.sort { |a,b| Versioncmp.compare( b, a ) }
end


def self.sort_version(array, direction)

def self.sort_version_by_method(array, method)
return nil if (array.nil? || array.empty?)
if direction.eql? "asc"
array.sort { |a,b| Versioncmp.compare( a, b ) }
else
array.sort { |a,b| Versioncmp.compare( b, a ) }
end
array.sort { |a,b| Versioncmp.compare(a.send(method), b.send(method)) }
end

def self.sort_version_by_method(array, method, direction)
def self.sort_version_by_method_desc(array, method)
return nil if (array.nil? || array.empty?)
if direction.eql? "asc"
array.sort { |a,b| Versioncmp.compare(a.send(method), b.send(method)) }
else
array.sort { |a,b| Versioncmp.compare(b.send(method), a.send(method)) }
end
array.sort { |a,b| Versioncmp.compare(b.send(method), a.send(method)) }
end



def self.get_newest_version(first, second)
array = [first, second]
array = array.sort { |a,b| Versioncmp.compare( a, b ) }
Expand Down
33 changes: 23 additions & 10 deletions spec/naturalsorter_spec.rb
Expand Up @@ -10,54 +10,67 @@
Naturalsorter::Sorter.sort(["a400", "a5", "a1"], true).should eql(["a1", "a5", "a400"])
end
end
describe "sort_desc" do
it "cba is abc" do
Naturalsorter::Sorter.sort_desc(["c", "b", "a"], true).should eql(["c", "b", "a"])
end
it "c400b5a1 is a1b5c400" do
Naturalsorter::Sorter.sort_desc(["a5", "a400", "a1"], true).should eql(["a400", "a5", "a1"])
end
end

describe "sort_by_method" do
it "c400b5a1 is a1b5c400" do
Naturalsorter::Sorter.sort_by_method(["a400", "a5", "a1"], "to_s", true).should eql(["a1", "a5", "a400"])
end
end
describe "sort_by_method_desc" do
it "a5 a400 a1 is a400 a5 a1" do
Naturalsorter::Sorter.sort_by_method_desc(["a5", "a400", "a1"], "to_s", true).should eql(["a400", "a5", "a1"])
end
end


describe "sort_version" do

it "1.1, 1.0 is 1.0, 1.1" do
Naturalsorter::Sorter.sort_version(["1.1", "1.0"], "asc").should eql(["1.0", "1.1"])
Naturalsorter::Sorter.sort_version(["1.1", "1.0"]).should eql(["1.0", "1.1"])
end

it "1.0, 1.1 is 1.0, 1.1" do
Naturalsorter::Sorter.sort_version(["1.0", "1.1"], "asc").should eql(["1.0", "1.1"])
Naturalsorter::Sorter.sort_version(["1.0", "1.1"]).should eql(["1.0", "1.1"])
end

it "4.5, 1.0 is 1.0, 4.5" do
Naturalsorter::Sorter.sort_version(["4.5", "1.0"], "asc").should eql(["1.0", "4.5"])
Naturalsorter::Sorter.sort_version(["4.5", "1.0"]).should eql(["1.0", "4.5"])
end

it "1.0, 4.5 is 1.0, 4.5" do
Naturalsorter::Sorter.sort_version(["1.0", "4.5"], "asc").should eql(["1.0", "4.5"])
Naturalsorter::Sorter.sort_version(["1.0", "4.5"]).should eql(["1.0", "4.5"])
end

it "1.2, 1.1 is 1.1, 1.2" do
Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0"], "asc").should eql(["0.1", "0.4", "1.0", "1.1", "1.2"])
Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0"]).should eql(["0.1", "0.4", "1.0", "1.1", "1.2"])
end

it "1.2, 1.1 is 1.1, 1.2" do
Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0", "1.0.RC1"], "asc").should eql(["0.1", "0.4", "1.0.RC1", "1.0", "1.1", "1.2"])
Naturalsorter::Sorter.sort_version( ["0.4", "0.1", "1.1", "1.2", "1.0", "1.0.RC1"]).should eql(["0.1", "0.4", "1.0.RC1", "1.0", "1.1", "1.2"])
end

it "1.2, 1.1 is 1.1, 1.2" do
Naturalsorter::Sorter.sort_version(["0.4", "0.1", "1.1", "1.2", "1.0", "1.0.RC1"], "desc").should eql(["1.2", "1.1", "1.0", "1.0.RC1", "0.4", "0.1"])
Naturalsorter::Sorter.sort_version_desc(["0.4", "0.1", "1.1", "1.2", "1.0", "1.0.RC1"]).should eql(["1.2", "1.1", "1.0", "1.0.RC1", "0.4", "0.1"])
end

it "1.2, 1.1 is 1.1, 1.2" do
Naturalsorter::Sorter.sort_version(["1.1", "1.2", "1.0"], "asc").should eql(["1.0", "1.1", "1.2"])
Naturalsorter::Sorter.sort_version(["1.1", "1.2", "1.0"]).should eql(["1.0", "1.1", "1.2"])
end

it "sort with RC" do
Naturalsorter::Sorter.sort_version(["1.1", "1.1.RC1"], "asc").should eql(["1.1.RC1", "1.1"])
Naturalsorter::Sorter.sort_version(["1.1", "1.1.RC1"]).should eql(["1.1.RC1", "1.1"])
end

it "sort with RC" do
Naturalsorter::Sorter.sort_version(["1.1.RC1", "1.1", "1.0"], "asc").should eql(["1.0", "1.1.RC1", "1.1"])
Naturalsorter::Sorter.sort_version(["1.1.RC1", "1.1", "1.0"]).should eql(["1.0", "1.1.RC1", "1.1"])
end

end
Expand Down

0 comments on commit 216ed3e

Please sign in to comment.