naturalsorter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in naturalsorter.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,14 @@
1
+ # NaturalSorter
2
+
3
+ This open source project is sorting arrays in a natural way. Assume you have an string array like this here
4
+
5
+ ["init30", "init20", "init200"]
6
+
7
+ If you are sorting this in ruby with ".sort" you will get this result
8
+
9
+ ["init20", "init200", "init30"]
10
+
11
+ Because the default sort method does not recognize the numbers in the string. The NaturalSorter will return this result.
12
+
13
+ ["init20", "init30", "init200"]
14
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/natcmp.rb ADDED
@@ -0,0 +1,76 @@
1
+ # natcmp.rb
2
+ #
3
+ # Natural order comparison of two strings
4
+ # e.g. "my_prog_v1.1.0" < "my_prog_v1.2.0" < "my_prog_v1.10.0"
5
+ # which does not follow alphabetically
6
+ #
7
+ # Based on Martin Pool's "Natural Order String Comparison" originally written in C
8
+ # http://sourcefrog.net/projects/natsort/
9
+ #
10
+ # This implementation is Copyright (C) 2003 by Alan Davies
11
+ # <cs96and_AT_yahoo_DOT_co_DOT_uk>
12
+ #
13
+ # This software is provided 'as-is', without any express or implied
14
+ # warranty. In no event will the authors be held liable for any damages
15
+ # arising from the use of this software.
16
+ #
17
+ # Permission is granted to anyone to use this software for any purpose,
18
+ # including commercial applications, and to alter it and redistribute it
19
+ # freely, subject to the following restrictions:
20
+ #
21
+ # 1. The origin of this software must not be misrepresented; you must not
22
+ # claim that you wrote the original software. If you use this software
23
+ # in a product, an acknowledgment in the product documentation would be
24
+ # appreciated but is not required.
25
+ # 2. Altered source versions must be plainly marked as such, and must not be
26
+ # misrepresented as being the original software.
27
+ # 3. This notice may not be removed or altered from any source distribution.
28
+
29
+ class Natcmp
30
+
31
+ # 'Natural order' comparison of two strings
32
+ def self.natcmp(str1, str2, caseInsensitive=false)
33
+ str1, str2 = str1.dup, str2.dup
34
+ compareExpression = /^(\D*)(\d*)(.*)$/
35
+
36
+ if caseInsensitive
37
+ str1.downcase!
38
+ str2.downcase!
39
+ end
40
+
41
+ # Remove all whitespace
42
+ str1.gsub!(/\s*/, '')
43
+ str2.gsub!(/\s*/, '')
44
+
45
+ while (str1.length > 0) or (str2.length > 0) do
46
+ # Extract non-digits, digits and rest of string
47
+ str1 =~ compareExpression
48
+ chars1, num1, str1 = $1.dup, $2.dup, $3.dup
49
+
50
+ str2 =~ compareExpression
51
+ chars2, num2, str2 = $1.dup, $2.dup, $3.dup
52
+
53
+ # Compare the non-digits
54
+ case (chars1 <=> chars2)
55
+ when 0 # Non-digits are the same, compare the digits...
56
+ # If either number begins with a zero, then compare alphabetically,
57
+ # otherwise compare numerically
58
+ if (num1[0] != 48) and (num2[0] != 48)
59
+ num1, num2 = num1.to_i, num2.to_i
60
+ end
61
+
62
+ case (num1 <=> num2)
63
+ when -1 then return -1
64
+ when 1 then return 1
65
+ end
66
+ when -1 then return -1
67
+ when 1 then return 1
68
+ end # case
69
+
70
+ end # while
71
+
72
+ # Strings are naturally equal
73
+ return 0
74
+ end
75
+
76
+ end
@@ -0,0 +1,3 @@
1
+ module Naturalsorter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ require "naturalsorter/version"
2
+ require "natcmp"
3
+
4
+ module Naturalsorter
5
+
6
+ class Sorter
7
+
8
+ def self.sort(array, caseinsesitive)
9
+ if (array.nil? || array.empty?)
10
+ return nil
11
+ end
12
+ array.sort { |a,b| Natcmp.natcmp(a,b,caseinsesitive) }
13
+ end
14
+
15
+ def self.sort_by_method(array, method, caseinsesitive)
16
+ if (array.nil? || array.empty?)
17
+ return nil
18
+ end
19
+ array.sort { |a,b| Natcmp.natcmp(a.send(method),b.send(method),caseinsesitive) }
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "naturalsorter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "naturalsorter"
7
+ s.version = Naturalsorter::VERSION
8
+ s.authors = ["reiz"]
9
+ s.email = ["robert.reiz@gmx.com"]
10
+ s.homepage = "http://robert-reiz.com"
11
+ s.summary = %q{Sorting arrays in natural order}
12
+ s.description = %q{This GEM is sorting Arrays in a natural order. a2 < a10}
13
+
14
+ s.rubyforge_project = "naturalsorter"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ s.add_development_dependency "rspec", "~> 2.6"
25
+ end
@@ -0,0 +1,17 @@
1
+ require "naturalsorter"
2
+
3
+ describe Naturalsorter::Sorter do
4
+
5
+ it "cba is abc" do
6
+ Naturalsorter::Sorter.sort(["c", "b", "a"], true).should eql(["a", "b", "c"])
7
+ end
8
+
9
+ it "c400b5a1 is a1b5c400" do
10
+ Naturalsorter::Sorter.sort(["c400", "b5", "a1"], true).should eql(["a1", "b5", "c400"])
11
+ end
12
+
13
+ it "c400b5a1 is a1b5c400" do
14
+ Naturalsorter::Sorter.sort_by_method(["c400", "b5", "a1"], "to_s", true).should eql(["a1", "b5", "c400"])
15
+ end
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: naturalsorter
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - reiz
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-01-08 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 6
30
+ version: "2.6"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: This GEM is sorting Arrays in a natural order. a2 < a10
34
+ email:
35
+ - robert.reiz@gmx.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.markdown
46
+ - Rakefile
47
+ - lib/natcmp.rb
48
+ - lib/naturalsorter.rb
49
+ - lib/naturalsorter/version.rb
50
+ - naturalsorter.gemspec
51
+ - spec/naturalsorter_spec.rb
52
+ has_rdoc: true
53
+ homepage: http://robert-reiz.com
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project: naturalsorter
78
+ rubygems_version: 1.3.6
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Sorting arrays in natural order
82
+ test_files:
83
+ - spec/naturalsorter_spec.rb