Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

docu and api

  • Loading branch information...
commit 216ed3ee0371634c8df90988020d6fefbea659c9 1 parent 45cbde9
reiz authored
Showing with 74 additions and 30 deletions.
  1. +29 −5 README.markdown
  2. +22 −15 lib/naturalsorter.rb
  3. +23 −10 spec/naturalsorter_spec.rb
View
34 README.markdown
@@ -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
@@ -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.
View
37 lib/naturalsorter.rb
@@ -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?)
@@ -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 ) }
View
33 spec/naturalsorter_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.