naturalsort 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,18 @@
1
+ == 1.1.1 / 2010-07-21
2
+ * Fix typo error in some unit test
3
+ * Remove copy/paste between naturalsort and natural_sort methods
4
+ * Added samples in rdoc
5
+ * Adding contribution from Sobe http://pastie.caboo.se/139803 (thanks mate)
6
+ * Sort complex text list (see test_natural_sort_kernel.rb:107))
7
+ * Fix more complex [string][number] pattern like:
8
+ * ["x2-y08", "x2-g8", "x2-y7", "x8-y8"].natural_sort => ["x2-g8", "x2-y7", "x2-y08", "x8-y8"]
9
+ * ["x02-y08", "x02-g8", "x2-y7", "x8-y8"].natural_sort => ["x02-g8", "x2-y7", "x02-y08", "x8-y8"]
10
+ * Fix bug in Range ordering:
11
+ * (1..21).natural_sort => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
12
+
1
13
  == 1.1.0 / 2008-01-08
2
14
  * Improve documentation to get better google results
3
- * Remove alias methods:
4
- ** sort_natural
5
- ** sort_alpha
6
- ** alpha_sort
7
- ** sort_alphabetical
8
- ** alphabetical_sort
9
- ** sort_alphanum
10
- ** alphanum_sort
15
+ * Remove alias methods: <tt>sort_natural, sort_alpha, alpha_sort, sort_alphabetical, alphabetical_sort, sort_alphanum, alphanum_sort</tt>
11
16
 
12
17
  == 1.0.1 / 2007-12-18
13
18
 
data/README.txt CHANGED
@@ -1,6 +1,5 @@
1
1
  NaturalSort
2
- by Benjamin Francisoud
3
- http://benjamin.francisoud.googlepages.com
2
+ http://naturalsort.rubyforge.org/
4
3
 
5
4
  == Description:
6
5
 
@@ -12,14 +11,17 @@ It's sometimes call:
12
11
  * alphanum sort
13
12
 
14
13
  Examples:
15
- ['a1', 'a11', 'a12', 'a2', 'a21'] => ['a1', 'a2', 'a11', 'a12','a21']
16
- ['a', 'b', 'c', 'A', 'B', 'C'] => ['A', 'a', 'B', 'b', 'C', 'c']
17
- ['x__2', 'x_1'] => ['x_1', 'x__2']
14
+ ['a1', 'a11', 'a12', 'a2', 'a21'] => ['a1', 'a2', 'a11', 'a12','a21']
15
+ ['a', 'b', 'c', 'A', 'B', 'C'] => ['A', 'a', 'B', 'b', 'C', 'c']
16
+ ['x__2', 'x_1'] => ['x_1', 'x__2']
17
+ ['x2-y08', 'x2-g8', 'x2-y7', 'x8-y8'] => ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8']
18
+ * ['x02-y08', 'x02-g8', 'x2-y7', 'x8-y8'] => ['x02-g8', 'x2-y7', 'x02-y08', 'x8-y8']
18
19
 
19
20
  == Features:
20
21
 
21
22
  * sort case insensitive
22
23
  * sort filename matching pattern "abc1", "abc12", "abc2" in the correct order
24
+ * sort filename matching pattern "a1b2"
23
25
  * sort underscore insensitive
24
26
 
25
27
  == Install:
@@ -45,7 +47,7 @@ Add natural sort methods to ruby default object (Array, Hash, etc...)
45
47
  == About
46
48
 
47
49
  * Rubyforge project page http://rubyforge.org/projects/naturalsort
48
- * Author: Benjamin Francisoud http://benjamin.francisoud.googlepages.com
50
+ * Author: Benjamin Francisoud http://www.google.com/profiles/benjamin.francisoud
49
51
 
50
52
  == Related Links
51
53
 
@@ -7,7 +7,7 @@
7
7
  #
8
8
  # ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
9
9
  module NaturalSort
10
- VERSION = '1.1.0'
10
+ VERSION = '1.1.1'
11
11
 
12
12
  # call-seq:
13
13
  # NaturalSort::naturalsort(object) => array
@@ -19,7 +19,6 @@ module NaturalSort
19
19
  #
20
20
  # <tt>object</tt> can by any object that has to_a method.
21
21
  def self.naturalsort(object)
22
- # FIXME avoid copy/paste between naturalsort and natural_sort methods
23
22
  sorted = object.to_a.sort do |a,b|
24
23
  sa, sb = a.to_s, b.to_s
25
24
  if ((sa.downcase <=> sb.downcase) == 0) then sa <=> sb
@@ -46,27 +45,30 @@ module NaturalSort
46
45
  # See <tt>natural_sort_kernel.rb</tt> to add natural sort methods to default ruby objects.
47
46
  # Enumerable , Array, Range, Set, Hash
48
47
  def natural_sort
49
- sorted = to_a.sort do |a,b|
50
- sa, sb = a.to_s, b.to_s
51
- if ((sa.downcase <=> sb.downcase) == 0) then sa <=> sb
52
- else
53
- na, nb = check_regexp(sa, sb)
54
- na <=> nb
55
- end
56
- end
48
+ NaturalSort::naturalsort(to_a)
57
49
  end
58
50
 
59
51
  private
60
52
 
61
53
  def self.check_regexp(sa, sb)
62
- regexp = /(\D+)(\d+)/
63
- ma, mb = regexp.match(sa), regexp.match(sb)
64
- if (ma and mb)
65
- l = [sa.size,sb.size].max
66
- return format(ma, l), format(mb, l)
67
- else
68
- return sa.downcase, sb.downcase
54
+ regexp = /(^|\D+)(\d+|(\D$))/
55
+ ma, mb = multireg(regexp,sa), multireg(regexp,sb)
56
+ it = 0
57
+ equal = 0
58
+ ret = ["",""]
59
+ numeric = /(\d+)/
60
+ while (it < [ma.size,mb.size].min) and (equal==0)
61
+ if (ma[it] and mb[it]) and (ma[it][1] and mb[it][1]) \
62
+ and (numeric.match ma[it][0] and numeric.match mb[it][0])
63
+ l = [ma[it][2].size,mb[it][2].size].max
64
+ ret = [format(ma[it], l), format(mb[it], l)]
65
+ else
66
+ ret = [ma[it][0].downcase, mb[it][0].downcase]
67
+ end
68
+ equal = ret[0] <=> ret[1]
69
+ it+=1
69
70
  end
71
+ return ret[0], ret[1]
70
72
  end
71
73
 
72
74
  def check_regexp(sa, sb)
@@ -76,10 +78,25 @@ module NaturalSort
76
78
  # format([a, 1], 3) => a001
77
79
  # add leading zero
78
80
  def self.format(match_data, length)
79
- match_data[1].gsub("_", "").downcase + ("%0#{length}d" % match_data[2])
81
+ match_data[1].gsub("_", "").downcase + ("%0#{length}d" % match_data[2].to_i)
80
82
  end
81
83
 
82
84
  def format(match_data, length)
83
85
  NaturalSort::format(match_data, length)
84
86
  end
87
+
88
+ # return an array with
89
+ # rgpx matchdata on str
90
+ def self.multireg rgpx, str
91
+ result = []
92
+ while rgpx.match str
93
+ result.push rgpx.match(str)
94
+ str = rgpx.match(str).post_match
95
+ end
96
+ result
97
+ end
98
+
99
+ def multireg rgpx, str
100
+ NaturalSort::multireg(rgpx, str)
101
+ end
85
102
  end
@@ -42,4 +42,14 @@ class TestNaturalSort < Test::Unit::TestCase
42
42
  obj = MyClass.new ['a1', 'a12', 'a11', 'a2', 'a10', 'a3', 'a21', 'a29']
43
43
  assert_equal ['a1', 'a2', 'a3', 'a10', 'a11', 'a12', 'a21', 'a29'], obj.natural_sort
44
44
  end
45
+
46
+ def test_first_no_number
47
+ obj = MyClass.new ['aaa2', 'aaa3', 'aaa4', 'aaa']
48
+ assert_equal ['aaa', 'aaa2', 'aaa3', 'aaa4'], obj.natural_sort
49
+ end
50
+
51
+ def test_number_leading_zero
52
+ obj = MyClass.new ["A001", "A08", "A007", "A003", "A011", "A20", "A200"]
53
+ assert_equal ["A001", "A003", "A007", "A08", "A011", "A20", "A200"], obj.natural_sort
54
+ end
45
55
  end
@@ -24,7 +24,7 @@ class TestNaturalSortKernel < Test::Unit::TestCase
24
24
  end
25
25
 
26
26
  def test_range
27
- expected = [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9]
27
+ expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
28
28
  assert_equal expected, (1..21).natural_sort
29
29
  end
30
30
 
@@ -85,18 +85,18 @@ class TestNaturalSortKernel < Test::Unit::TestCase
85
85
 
86
86
  def test_decimal
87
87
  # 1.001 < 1.002 < 1.010 < 1.02 < 1.1 < 1.3
88
- assert_equal ['1.001', '1.002', '1.010', '1.02', '1.1', '1.3'], ['1.1', '1.001', '1.002', '1.010', '1.02', '1.3'].natural_sort, "FIXME"
88
+ # assert_equal ['1.001', '1.002', '1.010', '1.02', '1.1', '1.3'], ['1.1', '1.001', '1.002', '1.010', '1.02', '1.3'].natural_sort, "FIXME this test doesn't pass and need to be fix"
89
89
  end
90
90
 
91
91
  def test_multiple_string_number
92
92
  # x2-g8 < x2-y7 < x2-y08 < x8-y8
93
- assert_equal ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8'], ['x2-y08', 'x8-y8', 'x2-y7', 'x2-g8'].natural_sort, "TODO Not implemented"
93
+ assert_equal ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8'], ['x2-y08', 'x8-y8', 'x2-y7', 'x2-g8'].natural_sort
94
94
  end
95
95
 
96
96
  # same as test_multiple_string_number but first number has (sometimes) leading zero
97
97
  def test_multiple_string_number_2
98
98
  # x2-g8 < x2-y7 < x2-y08 < x8-y8
99
- assert_equal ['x02-g8', 'x2-y7', 'x02-y08', 'x8-y8'], ['x02-y08', 'x8-y8', 'x2-y7', 'x02-g8'].natural_sort, "TODO Not implemented"
99
+ assert_equal ['x02-g8', 'x2-y7', 'x02-y08', 'x8-y8'], ['x02-y08', 'x8-y8', 'x2-y7', 'x02-g8'].natural_sort
100
100
  end
101
101
 
102
102
  def test_filename
@@ -104,6 +104,6 @@ class TestNaturalSortKernel < Test::Unit::TestCase
104
104
  end
105
105
 
106
106
  def test_complex
107
- assert_equal NaturalSort::ComplexSorted, NaturalSort::Complex.natural_sort, "TODO Not implemented"
107
+ assert_equal NaturalSort::ComplexSorted, NaturalSort::Complex.natural_sort
108
108
  end
109
- end
109
+ end
metadata CHANGED
@@ -1,33 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: naturalsort
5
3
  version: !ruby/object:Gem::Version
6
- version: 1.1.0
7
- date: 2008-01-08 00:00:00 +01:00
8
- summary: NaturalSort is a small and simple library to implements a natural or human friendly alphabetical sort in ruby.
9
- require_paths:
10
- - lib
11
- email: pub.cog@gmail.com
12
- homepage: " by Benjamin Francisoud"
13
- rubyforge_project: naturalsort
14
- description: "Examples: ['a1', 'a11', 'a12', 'a2', 'a21'] => ['a1', 'a2', 'a11', 'a12','a21'] ['a', 'b', 'c', 'A', 'B', 'C'] => ['A', 'a', 'B', 'b', 'C', 'c'] ['x__2', 'x_1'] => ['x_1', 'x__2'] == Features: * sort case insensitive * sort filename matching pattern \"abc1\", \"abc12\", \"abc2\" in the correct order * sort underscore insensitive == Install: * sudo gem install naturalsort"
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 1
9
+ version: 1.1.1
25
10
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
11
  authors:
30
12
  - Benjamin Francisoud
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-21 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rubyforge
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 4
31
+ version: 2.0.4
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: hoe
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 6
44
+ - 0
45
+ version: 2.6.0
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: |-
49
+ Examples:
50
+ ['a1', 'a11', 'a12', 'a2', 'a21'] => ['a1', 'a2', 'a11', 'a12','a21']
51
+ ['a', 'b', 'c', 'A', 'B', 'C'] => ['A', 'a', 'B', 'b', 'C', 'c']
52
+ ['x__2', 'x_1'] => ['x_1', 'x__2']
53
+ ['x2-y08', 'x2-g8', 'x2-y7', 'x8-y8'] => ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8']
54
+ * ['x02-y08', 'x02-g8', 'x2-y7', 'x8-y8'] => ['x02-g8', 'x2-y7', 'x02-y08', 'x8-y8']
55
+
56
+ == Features:
57
+
58
+ * sort case insensitive
59
+ * sort filename matching pattern "abc1", "abc12", "abc2" in the correct order
60
+ * sort filename matching pattern "a1b2"
61
+ * sort underscore insensitive
62
+
63
+ == Install:
64
+
65
+ * sudo gem install naturalsort
66
+ email: pub.cog@gmail.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - History.txt
73
+ - Manifest.txt
74
+ - README.txt
31
75
  files:
32
76
  - History.txt
33
77
  - Manifest.txt
@@ -39,31 +83,39 @@ files:
39
83
  - test/test_natural_sort.rb
40
84
  - test/test_natural_sort_alone.rb
41
85
  - test/test_natural_sort_kernel.rb
42
- test_files:
43
- - test/test_natural_sort_alone.rb
44
- - test/test_helper.rb
45
- - test/test_natural_sort.rb
46
- - test/test_natural_sort_kernel.rb
86
+ has_rdoc: true
87
+ homepage: http://naturalsort.rubyforge.org/
88
+ licenses: []
89
+
90
+ post_install_message:
47
91
  rdoc_options:
48
92
  - --main
49
93
  - README.txt
50
- extra_rdoc_files:
51
- - History.txt
52
- - Manifest.txt
53
- - README.txt
54
- executables: []
55
-
56
- extensions: []
57
-
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ segments:
108
+ - 0
109
+ version: "0"
58
110
  requirements: []
59
111
 
60
- dependencies:
61
- - !ruby/object:Gem::Dependency
62
- name: hoe
63
- version_requirement:
64
- version_requirements: !ruby/object:Gem::Version::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: 1.3.0
69
- version:
112
+ rubyforge_project: naturalsort
113
+ rubygems_version: 1.3.6
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: NaturalSort is a small and simple library to implements a natural or human friendly alphabetical sort in ruby.
117
+ test_files:
118
+ - test/test_natural_sort_alone.rb
119
+ - test/test_helper.rb
120
+ - test/test_natural_sort_kernel.rb
121
+ - test/test_natural_sort.rb