naturalsort 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +1 -0
- data/README.txt +4 -4
- data/Rakefile +13 -0
- data/lib/natural_sort.rb +36 -16
- data/test/test_helper.rb +23 -0
- data/test/test_natural_sort_alone.rb +9 -0
- data/test/test_natural_sort_kernel.rb +24 -0
- metadata +5 -3
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
NaturalSort
|
2
|
-
|
3
|
-
|
2
|
+
http://rubyforge.org/projects/naturalsort
|
3
|
+
by Benjamin Francisoud http://benjamin.francisoud.googlepages.com
|
4
4
|
|
5
5
|
== Description:
|
6
6
|
|
@@ -39,7 +39,7 @@ Pick your favorite method name: NaturalSort
|
|
39
39
|
|
40
40
|
== Install:
|
41
41
|
|
42
|
-
* sudo gem install
|
42
|
+
* sudo gem install naturalsort
|
43
43
|
|
44
44
|
== Related Links
|
45
45
|
|
@@ -51,7 +51,7 @@ Links related to the alphabetical sorting problem:
|
|
51
51
|
* http://www.davekoelle.com/alphanum.html
|
52
52
|
* http://rikkus.info/arch/sensible_sort.rb
|
53
53
|
* http://weblog.masukomi.org/2007/12/10/alphabetical-asciibetical
|
54
|
-
* http://nedbatchelder.com/blog/200712.html
|
54
|
+
* http://nedbatchelder.com/blog/200712.html#e20071211T054956
|
55
55
|
|
56
56
|
== License:
|
57
57
|
|
data/Rakefile
CHANGED
@@ -16,4 +16,17 @@ Hoe.new('naturalsort', NaturalSort::VERSION) do |p|
|
|
16
16
|
p.need_zip = true
|
17
17
|
end
|
18
18
|
|
19
|
+
SVN = 'svn+ssh://cogito@rubyforge.org/var/svn/naturalsort'
|
20
|
+
|
21
|
+
# FIXME
|
22
|
+
task :svn_branch => [:clean] do
|
23
|
+
`svn delete #{SVN}/branches/1.X -m "cleanup branch"`
|
24
|
+
`svn copy #{SVN}/trunk #{SVN}/branches/1.X -m "create branch"`
|
25
|
+
end
|
26
|
+
|
27
|
+
# FIXME
|
28
|
+
task :svn_tag do
|
29
|
+
`svn copy #{SVN}/trunk #{SVN}/tags/#{NaturalSort::VERSION} -m "create tag"`
|
30
|
+
end
|
31
|
+
|
19
32
|
# vim: syntax=Ruby
|
data/lib/natural_sort.rb
CHANGED
@@ -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.0.
|
10
|
+
VERSION = '1.0.1'
|
11
11
|
|
12
12
|
# call-seq:
|
13
13
|
# NaturalSort::naturalsort(object) => array
|
@@ -15,11 +15,19 @@ module NaturalSort
|
|
15
15
|
# Static method to sort.
|
16
16
|
#
|
17
17
|
# *Usage*
|
18
|
-
# naturalsort ['a1', 'a12', 'a2']
|
18
|
+
# NaturalSort::naturalsort ['a1', 'a12', 'a2'] #=> ['a1', 'a2', 'a12']
|
19
19
|
#
|
20
|
-
# <tt>object</tt> can by any object that has to_a
|
20
|
+
# <tt>object</tt> can by any object that has to_a method.
|
21
21
|
def self.naturalsort(object)
|
22
|
-
|
22
|
+
# FIXME avoid copy/paste between naturalsort and natural_sort methods
|
23
|
+
sorted = object.to_a.sort do |a,b|
|
24
|
+
sa, sb = a.to_s, b.to_s
|
25
|
+
if ((sa.downcase <=> sb.downcase) == 0) then sa <=> sb
|
26
|
+
else
|
27
|
+
na, nb = check_regexp(sa, sb)
|
28
|
+
na <=> nb
|
29
|
+
end
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
33
|
# call-seq:
|
@@ -40,18 +48,10 @@ module NaturalSort
|
|
40
48
|
def natural_sort
|
41
49
|
sorted = to_a.sort do |a,b|
|
42
50
|
sa, sb = a.to_s, b.to_s
|
43
|
-
if ((sa.downcase <=> sb.downcase) == 0)
|
44
|
-
sa <=> sb
|
51
|
+
if ((sa.downcase <=> sb.downcase) == 0) then sa <=> sb
|
45
52
|
else
|
46
|
-
|
47
|
-
|
48
|
-
if (ma and mb)
|
49
|
-
l = [sa.size,sb.size].max
|
50
|
-
na, nb = format(ma, l), format(mb, l)
|
51
|
-
na <=> nb
|
52
|
-
else
|
53
|
-
sa.downcase <=> sb.downcase
|
54
|
-
end
|
53
|
+
na, nb = check_regexp(sa, sb)
|
54
|
+
na <=> nb
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
@@ -65,9 +65,29 @@ module NaturalSort
|
|
65
65
|
alias alphanum_sort natural_sort
|
66
66
|
|
67
67
|
private
|
68
|
+
|
69
|
+
def self.check_regexp(sa, sb)
|
70
|
+
regexp = /(\D+)(\d+)/
|
71
|
+
ma, mb = regexp.match(sa), regexp.match(sb)
|
72
|
+
if (ma and mb)
|
73
|
+
l = [sa.size,sb.size].max
|
74
|
+
return format(ma, l), format(mb, l)
|
75
|
+
else
|
76
|
+
return sa.downcase, sb.downcase
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def check_regexp(sa, sb)
|
81
|
+
NaturalSort::check_regexp(sa, sb)
|
82
|
+
end
|
83
|
+
|
68
84
|
# format([a, 1], 3) => a001
|
69
85
|
# add leading zero
|
70
|
-
def format(match_data, length)
|
86
|
+
def self.format(match_data, length)
|
71
87
|
match_data[1].gsub("_", "").downcase + ("%0#{length}d" % match_data[2])
|
72
88
|
end
|
89
|
+
|
90
|
+
def format(match_data, length)
|
91
|
+
NaturalSort::format(match_data, length)
|
92
|
+
end
|
73
93
|
end
|
data/test/test_helper.rb
CHANGED
@@ -3,4 +3,27 @@ require 'test/unit'
|
|
3
3
|
module NaturalSort
|
4
4
|
Base = ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D']
|
5
5
|
BaseSorted = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd']
|
6
|
+
|
7
|
+
Complex = ['1000X Radonius Maximus', '10X Radonius', '200X Radonius', '20X Radonius',
|
8
|
+
'20X Radonius Prime', '30X Radonius', '40X Radonius', 'Allegia 50 Clasteron',
|
9
|
+
'Allegia 500 Clasteron', 'Allegia 51 Clasteron', 'Allegia 51B Clasteron', 'Allegia 52 Clasteron',
|
10
|
+
'Allegia 60 Clasteron', 'Alpha 100', 'Alpha 2', 'Alpha 200', 'Alpha 2A', 'Alpha 2A-8000',
|
11
|
+
'Alpha 2A-900', 'Callisto Morphamax', 'Callisto Morphamax 500', 'Callisto Morphamax 5000',
|
12
|
+
'Callisto Morphamax 600', 'Callisto Morphamax 700', 'Callisto Morphamax 7000',
|
13
|
+
'Callisto Morphamax 7000 SE', 'Callisto Morphamax 7000 SE2', 'QRS-60 Intrinsia Machine',
|
14
|
+
'QRS-60F Intrinsia Machine', 'QRS-62 Intrinsia Machine', 'QRS-62F Intrinsia Machine',
|
15
|
+
'Xiph Xlater 10000', 'Xiph Xlater 2000', 'Xiph Xlater 300', 'Xiph Xlater 40',
|
16
|
+
'Xiph Xlater 5', 'Xiph Xlater 50', 'Xiph Xlater 500', 'Xiph Xlater 5000', 'Xiph Xlater 58']
|
17
|
+
|
18
|
+
ComplexSorted = ['10X Radonius', '20X Radonius', '20X Radonius Prime', '30X Radonius',
|
19
|
+
'40X Radonius', '200X Radonius', '1000X Radonius Maximus', 'Allegia 50 Clasteron',
|
20
|
+
'Allegia 51 Clasteron', 'Allegia 51B Clasteron', 'Allegia 52 Clasteron',
|
21
|
+
'Allegia 60 Clasteron', 'Allegia 500 Clasteron', 'Alpha 2', 'Alpha 2A', 'Alpha 2A-900',
|
22
|
+
'Alpha 2A-8000', 'Alpha 100', 'Alpha 200', 'Callisto Morphamax', 'Callisto Morphamax 500',
|
23
|
+
'Callisto Morphamax 600', 'Callisto Morphamax 700', 'Callisto Morphamax 5000',
|
24
|
+
'Callisto Morphamax 7000', 'Callisto Morphamax 7000 SE', 'Callisto Morphamax 7000 SE2',
|
25
|
+
'QRS-60 Intrinsia Machine', 'QRS-60F Intrinsia Machine', 'QRS-62 Intrinsia Machine',
|
26
|
+
'QRS-62F Intrinsia Machine', 'Xiph Xlater 5', 'Xiph Xlater 40', 'Xiph Xlater 50',
|
27
|
+
'Xiph Xlater 58', 'Xiph Xlater 300', 'Xiph Xlater 500', 'Xiph Xlater 2000',
|
28
|
+
'Xiph Xlater 5000', 'Xiph Xlater 10000']
|
6
29
|
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/natural_sort.rb'
|
3
|
+
|
4
|
+
# Test without include
|
5
|
+
class TestNaturalSortAlone < Test::Unit::TestCase
|
6
|
+
def test_alone
|
7
|
+
assert_equal NaturalSort::BaseSorted, NaturalSort::naturalsort(NaturalSort::Base)
|
8
|
+
end
|
9
|
+
end
|
@@ -84,4 +84,28 @@ class TestNaturalSortKernel < Test::Unit::TestCase
|
|
84
84
|
['hello world', 'hello world 2', 'hello 2 world'].natural_sort
|
85
85
|
)
|
86
86
|
end
|
87
|
+
|
88
|
+
def test_decimal
|
89
|
+
# 1.001 < 1.002 < 1.010 < 1.02 < 1.1 < 1.3
|
90
|
+
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"
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_multiple_string_number
|
94
|
+
# x2-g8 < x2-y7 < x2-y08 < x8-y8
|
95
|
+
assert_equal ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8'], ['x2-y08', 'x8-y8', 'x2-y7', 'x2-g8'].natural_sort, "TODO Not implemented"
|
96
|
+
end
|
97
|
+
|
98
|
+
# same as test_multiple_string_number but first number has (sometimes) leading zero
|
99
|
+
def test_multiple_string_number_2
|
100
|
+
# x2-g8 < x2-y7 < x2-y08 < x8-y8
|
101
|
+
assert_equal ['x02-g8', 'x2-y7', 'x02-y08', 'x8-y8'], ['x02-y08', 'x8-y8', 'x2-y7', 'x02-g8'].natural_sort, "TODO Not implemented"
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_filename
|
105
|
+
assert_equal ["img1.png", "img2.png", "img10.png", "img12.png"], ["img12.png", "img10.png", "img2.png", "img1.png"].natural_sort
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_complex
|
109
|
+
assert_equal NaturalSort::ComplexSorted, NaturalSort::Complex.natural_sort, "TODO Not implemented"
|
110
|
+
end
|
87
111
|
end
|
metadata
CHANGED
@@ -3,13 +3,13 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: naturalsort
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2007-12-
|
6
|
+
version: 1.0.1
|
7
|
+
date: 2007-12-18 00:00:00 +01:00
|
8
8
|
summary: NaturalSort is a small and simple library to implements a natural or human friendly alphabetical sort in ruby.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: pub.cog@gmail.com
|
12
|
-
homepage: "
|
12
|
+
homepage: " http://rubyforge.org/projects/naturalsort"
|
13
13
|
rubyforge_project: naturalsort
|
14
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 == Synopsis: === Usage N\xB01 - Add to your ruby default objects Add natural sort methods to ruby default object (Array, Hash, etc...)"
|
15
15
|
autorequire:
|
@@ -37,8 +37,10 @@ files:
|
|
37
37
|
- lib/natural_sort_kernel.rb
|
38
38
|
- test/test_helper.rb
|
39
39
|
- test/test_natural_sort.rb
|
40
|
+
- test/test_natural_sort_alone.rb
|
40
41
|
- test/test_natural_sort_kernel.rb
|
41
42
|
test_files:
|
43
|
+
- test/test_natural_sort_alone.rb
|
42
44
|
- test/test_helper.rb
|
43
45
|
- test/test_natural_sort.rb
|
44
46
|
- test/test_natural_sort_kernel.rb
|