naturalsorter 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +43 -3
- data/lib/naturalsorter/version.rb +1 -1
- data/lib/naturalsorter.rb +24 -0
- data/naturalsorter.gemspec +2 -2
- data/spec/naturalsorter_spec.rb +2 -2
- metadata +3 -3
data/README.markdown
CHANGED
@@ -1,14 +1,54 @@
|
|
1
1
|
# NaturalSorter
|
2
2
|
|
3
|
+
## The Mission
|
4
|
+
|
3
5
|
This open source project is sorting arrays in a natural way. Assume you have an string array like this here
|
4
6
|
|
5
|
-
["init30", "init20", "init200"]
|
7
|
+
`["init30", "init20", "init200"]`
|
6
8
|
|
7
9
|
If you are sorting this in ruby with ".sort" you will get this result
|
8
10
|
|
9
|
-
["init20", "init200", "init30"]
|
11
|
+
`["init20", "init200", "init30"]`
|
10
12
|
|
11
13
|
Because the default sort method does not recognize the numbers in the string. The NaturalSorter will return this result.
|
12
14
|
|
13
|
-
["init20", "init30", "init200"]
|
15
|
+
`["init20", "init30", "init200"]`
|
16
|
+
|
17
|
+
## API
|
18
|
+
|
19
|
+
This GEM has just 2 methods
|
20
|
+
|
21
|
+
`Naturalsorter::Sorter.sort(array, caseinsesitive)`
|
22
|
+
|
23
|
+
And this here for more advanced sorting
|
24
|
+
|
25
|
+
`Naturalsorter::Sorter.sort_by_method(array, method, caseinsesitive)`
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
You should add this line to your Gemfile
|
30
|
+
|
31
|
+
`gem 'naturalsorter', '0.1.0'`
|
32
|
+
|
33
|
+
and run this command in your app root directory
|
34
|
+
|
35
|
+
`bundle install`
|
36
|
+
|
37
|
+
## How To Use - Examples
|
38
|
+
|
39
|
+
After the installation you can use it like this:
|
40
|
+
|
41
|
+
`Naturalsorter::Sorter.sort(["a400", "a5", "a1"], true)`
|
42
|
+
|
43
|
+
it will return the array ["a1", "a5", "a400"].
|
44
|
+
|
45
|
+
If you have more advanced objects you want to sort, you should use the second method. Assume you have a Class User with 3 attributes: "firstname", "lastname", "age" and you want to sort an array of class Users by "firstname".
|
46
|
+
|
47
|
+
`Naturalsorter::Sorter.sort_by_method(users, "firstname", true)`
|
48
|
+
|
49
|
+
that's it!
|
50
|
+
|
51
|
+
## Alan Davies
|
52
|
+
|
53
|
+
This project uses internal the natcmp implementation from Alan Davies. All glorry to him for his awesome work.
|
14
54
|
|
data/lib/naturalsorter.rb
CHANGED
@@ -1,10 +1,33 @@
|
|
1
1
|
require "naturalsorter/version"
|
2
2
|
require "natcmp"
|
3
3
|
|
4
|
+
# naturalsorter.rb
|
5
|
+
#
|
6
|
+
# Natural order sorting for an array of strings or more advanced objects
|
7
|
+
#
|
8
|
+
# This implementation is Copyright (C) 2012 by Robert Reiz
|
9
|
+
#
|
10
|
+
# This software is provided 'as-is', without any express or implied
|
11
|
+
# warranty. In no event will the authors be held liable for any damages
|
12
|
+
# arising from the use of this software.
|
13
|
+
#
|
14
|
+
# Permission is granted to anyone to use this software for any purpose,
|
15
|
+
# including commercial applications, and to alter it and redistribute it
|
16
|
+
# freely, subject to the following restrictions:
|
17
|
+
#
|
18
|
+
# 1. The origin of this software must not be misrepresented; you must not
|
19
|
+
# claim that you wrote the original software. If you use this software
|
20
|
+
# in a product, an acknowledgment in the product documentation would be
|
21
|
+
# appreciated but is not required.
|
22
|
+
# 2. Altered source versions must be plainly marked as such, and must not be
|
23
|
+
# misrepresented as being the original software.
|
24
|
+
# 3. This notice may not be removed or altered from any source distribution.
|
25
|
+
|
4
26
|
module Naturalsorter
|
5
27
|
|
6
28
|
class Sorter
|
7
29
|
|
30
|
+
# 'Natural order' sort for an simple string array
|
8
31
|
def self.sort(array, caseinsesitive)
|
9
32
|
if (array.nil? || array.empty?)
|
10
33
|
return nil
|
@@ -12,6 +35,7 @@ module Naturalsorter
|
|
12
35
|
array.sort { |a,b| Natcmp.natcmp(a,b,caseinsesitive) }
|
13
36
|
end
|
14
37
|
|
38
|
+
# 'Natural order' sort for an array of objects.
|
15
39
|
def self.sort_by_method(array, method, caseinsesitive)
|
16
40
|
if (array.nil? || array.empty?)
|
17
41
|
return nil
|
data/naturalsorter.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = Naturalsorter::VERSION
|
8
8
|
s.authors = ["reiz"]
|
9
9
|
s.email = ["robert.reiz@gmx.com"]
|
10
|
-
s.homepage = "
|
10
|
+
s.homepage = "https://github.com/reiz/naturalsorter"
|
11
11
|
s.summary = %q{Sorting arrays in natural order}
|
12
12
|
s.description = %q{This GEM is sorting Arrays in a natural order. a2 < a10}
|
13
13
|
|
@@ -22,4 +22,4 @@ Gem::Specification.new do |s|
|
|
22
22
|
# s.add_development_dependency "rspec"
|
23
23
|
# s.add_runtime_dependency "rest-client"
|
24
24
|
s.add_development_dependency "rspec", "~> 2.6"
|
25
|
-
end
|
25
|
+
end
|
data/spec/naturalsorter_spec.rb
CHANGED
@@ -7,11 +7,11 @@ describe Naturalsorter::Sorter do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "c400b5a1 is a1b5c400" do
|
10
|
-
Naturalsorter::Sorter.sort(["
|
10
|
+
Naturalsorter::Sorter.sort(["a400", "a5", "a1"], true).should eql(["a1", "a5", "a400"])
|
11
11
|
end
|
12
12
|
|
13
13
|
it "c400b5a1 is a1b5c400" do
|
14
|
-
Naturalsorter::Sorter.sort_by_method(["
|
14
|
+
Naturalsorter::Sorter.sort_by_method(["a400", "a5", "a1"], "to_s", true).should eql(["a1", "a5", "a400"])
|
15
15
|
end
|
16
16
|
|
17
17
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 0
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- reiz
|
@@ -50,7 +50,7 @@ files:
|
|
50
50
|
- naturalsorter.gemspec
|
51
51
|
- spec/naturalsorter_spec.rb
|
52
52
|
has_rdoc: true
|
53
|
-
homepage:
|
53
|
+
homepage: https://github.com/reiz/naturalsorter
|
54
54
|
licenses: []
|
55
55
|
|
56
56
|
post_install_message:
|