naturalsort 1.1.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +56 -0
- data/LICENSE +22 -0
- data/README.md +121 -0
- data/lib/natural_sort/base.rb +70 -0
- data/lib/natural_sort/engine.rb +87 -0
- data/lib/natural_sort/kernel.rb +57 -0
- data/lib/natural_sort/version.rb +7 -0
- data/lib/natural_sort.rb +6 -102
- data/lib/natural_sort_kernel.rb +3 -46
- metadata +64 -104
- data/History.txt +0 -24
- data/Manifest.txt +0 -10
- data/README.txt +0 -87
- data/Rakefile +0 -30
- data/test/test_helper.rb +0 -29
- data/test/test_natural_sort.rb +0 -55
- data/test/test_natural_sort_alone.rb +0 -9
- data/test/test_natural_sort_kernel.rb +0 -109
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 587a1c0cf0b42077845643ce2ad013bead1f21bb57418446bf222195385dab37
|
4
|
+
data.tar.gz: 9cc750189f4fc09d05e2c660fa1e6cfc25a4c098d5c9517880766e72e3e9b1a3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c24a24d6dac5e791157df9c868aebcfd1b8134480b197bd66a14536c9d14a2ae5174d22852346ab1c2c8c5938ace451913fc5c37d7c7af4b5ed613db1f740292
|
7
|
+
data.tar.gz: 0fb932df47946a2799e3defaeae455ffcf72dcc08b66f6589dbcb62c0b656b6990c822b4d05f0014853f0d2a642f31469172d506951053ec8b030570230ec604
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 1.3.0 - 2024-10-20
|
4
|
+
|
5
|
+
* Remove `NaturalSort::Kernel` module.
|
6
|
+
* Drop support for Ruby 2.7 and prior.
|
7
|
+
* Add Github Actions and remove Travis CI.
|
8
|
+
* Fixes issue where blank string breaks sorting.
|
9
|
+
|
10
|
+
## 1.2.0 - 2013-10-10
|
11
|
+
|
12
|
+
* DEPRECATION: deprecate `NaturalSort.naturalsort` and replace with `NaturalSort.sort`
|
13
|
+
* Use `Array(object)` instead of `object.to_a` for implicit Array conversion
|
14
|
+
* Remove unused private instance methods which were aliased to class methods
|
15
|
+
* Make regexp objects as module constants
|
16
|
+
* Reorganize directory/require structure to match conventions used in other gems. Core sorting functions are moved to new `NaturalSort::Engine` module.
|
17
|
+
* Kernel: Only include into `Set` if Set is defined
|
18
|
+
* Kernel: `Range#natural_sort` is now an alias to `Range#to_a`
|
19
|
+
* Test: Upgrade to use Minitest gem
|
20
|
+
* Test: Improve readability by using `%w()` syntax for arrays
|
21
|
+
* Documentation: Convert all code documentation to TomDoc format
|
22
|
+
* Admin: Remove rdoc generator which was not working
|
23
|
+
* Admin: Add files .gitignore, Gemfile, gemspec, and cleanup Rakefile
|
24
|
+
* Admin: Add Travis and CodeClimate
|
25
|
+
* Admin: Migrate to Github
|
26
|
+
|
27
|
+
## 1.1.1 - 2010-07-21
|
28
|
+
|
29
|
+
* Fix typo error in some unit test
|
30
|
+
* Remove copy/paste between naturalsort and natural_sort methods
|
31
|
+
* Added samples in rdoc
|
32
|
+
* Adding contribution from Sobe http://pastie.caboo.se/139803 (thanks mate)
|
33
|
+
* Sort complex text list (see test_natural_sort_kernel.rb:107))
|
34
|
+
* Fix more complex [string][number] pattern like:
|
35
|
+
```ruby
|
36
|
+
["x2-y08", "x2-g8", "x2-y7", "x8-y8"].natural_sort => ["x2-g8", "x2-y7", "x2-y08", "x8-y8"]
|
37
|
+
["x02-y08", "x02-g8", "x2-y7", "x8-y8"].natural_sort => ["x02-g8", "x2-y7", "x02-y08", "x8-y8"]
|
38
|
+
```
|
39
|
+
* Fix bug in Range ordering:
|
40
|
+
```ruby
|
41
|
+
(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]
|
42
|
+
```
|
43
|
+
|
44
|
+
## 1.1.0 - 2008-01-08
|
45
|
+
|
46
|
+
* Improve documentation to get better google results
|
47
|
+
* Remove alias methods: `sort_natural`, `sort_alpha`, `alpha_sort`, `sort_alphabetical`, `alphabetical_sort`, `sort_alphanum`, `alphanum_sort`
|
48
|
+
|
49
|
+
## 1.0.1 - 2007-12-18
|
50
|
+
|
51
|
+
* Fix problem using as a static method
|
52
|
+
* Refactoring code
|
53
|
+
|
54
|
+
## 1.0.0 - 2007-12-14
|
55
|
+
|
56
|
+
* Project creation
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2007 Benjamin Francisoud
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# NaturalSort
|
2
|
+
|
3
|
+
NaturalSort is a simple library which implements a natural, human-friendly alphanumeric sort in Ruby.
|
4
|
+
|
5
|
+
## Examples
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
%w[a1 a11 a12 a2 a21].natural_sort #=> %w[a1 a2 a11 a12 a21]
|
9
|
+
%w[a b c A B C].natural_sort #=> %w[A a B b C c]
|
10
|
+
%w[x__2 x_1].natural_sort #=> %w[x_1 x__2]
|
11
|
+
%w[x2-y08 x2-g8 x2-y7 x8-y8].natural_sort #=> %w[x2-g8 x2-y7 x2-y08 x8-y8]
|
12
|
+
%w[x02-y08 x02-g8 x2-y7 x8-y8].natural_sort #=> %w[x02-g8 x2-y7 x02-y08 x8-y8]
|
13
|
+
```
|
14
|
+
|
15
|
+
## Features
|
16
|
+
|
17
|
+
* Sort case insensitive
|
18
|
+
* Sort underscore insensitive
|
19
|
+
* Sort filename matching patterns
|
20
|
+
* Sort mixed alpha and numeric "abc1", "abc12", "abc2", "a1b2" in correct order
|
21
|
+
|
22
|
+
## Install
|
23
|
+
|
24
|
+
#### With Bundler
|
25
|
+
|
26
|
+
In your `Gemfile`:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem 'naturalsort'
|
30
|
+
```
|
31
|
+
|
32
|
+
or to optionally extend Ruby native objects:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
gem 'naturalsort', require: 'natural_sort_kernel'
|
36
|
+
```
|
37
|
+
|
38
|
+
#### From Command Line
|
39
|
+
|
40
|
+
```cmd
|
41
|
+
$ gem install naturalsort
|
42
|
+
```
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
#### Extend Ruby native enumerable objects
|
47
|
+
|
48
|
+
`require 'natural_sort_kernel'` adds `natural_sort` methods to all native Ruby enumerable objects (Array, Hash, etc...)
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'natural_sort_kernel'
|
52
|
+
|
53
|
+
%w[a b c A B C].natural_sort
|
54
|
+
```
|
55
|
+
|
56
|
+
#### Use as a module function
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'natural_sort' # unless using Bundler
|
60
|
+
|
61
|
+
NaturalSort.sort %w[a b c d A B C D]
|
62
|
+
```
|
63
|
+
|
64
|
+
#### Use comparator function as a standalone
|
65
|
+
|
66
|
+
Adds `natural_sort` methods to Ruby native enumerable objects (Array, Hash, etc...)
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
person_1 = Person.new('Moe')
|
70
|
+
person_2 = Person.new('Larry')
|
71
|
+
person_3 = Person.new('Curly')
|
72
|
+
|
73
|
+
[person_1, person_2, person_3].sort{|a,b| NaturalSort.comparator(a.name, b.name)} #=> [person_3, person_2, person_1]
|
74
|
+
|
75
|
+
%w[a b c A B C].natural_sort
|
76
|
+
```
|
77
|
+
|
78
|
+
#### Include into your own objects
|
79
|
+
|
80
|
+
Can be used to add `#natural_sort` method to on any enumerable object or any object which implements `#to_a`
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class TodoList < Array
|
84
|
+
include NaturalSort
|
85
|
+
end
|
86
|
+
|
87
|
+
todo_list = TodoList.new
|
88
|
+
todo_list << 'Wash car'
|
89
|
+
todo_list << 'Water plants'
|
90
|
+
todo_list << 'Feed dog'
|
91
|
+
|
92
|
+
todo_list.natural_sort #=> ['Feed dog', 'Wash car', 'Water plants']
|
93
|
+
```
|
94
|
+
|
95
|
+
## Authors
|
96
|
+
|
97
|
+
* [Benjamin Francisoud](http://www.google.com/profiles/benjamin.francisoud)
|
98
|
+
* [Johnny Shields](http://github.com/johnnyshields) (version 1.2.0+)
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
Fork -> Patch -> Spec -> Push -> Pull Request
|
103
|
+
|
104
|
+
## Related Links
|
105
|
+
|
106
|
+
Links related to the natural sorting problem:
|
107
|
+
|
108
|
+
* http://www.codinghorror.com/blog/archives/001018.html
|
109
|
+
* http://sourcefrog.net/projects/natsort/
|
110
|
+
* http://be2.php.net/natsort
|
111
|
+
* http://www.wellho.net/forum/Programming-in-Python-and-Ruby/Python-Sort-Order.html
|
112
|
+
* http://www.davekoelle.com/alphanum.html
|
113
|
+
* http://rikkus.info/arch/sensible_sort.rb
|
114
|
+
* http://weblog.masukomi.org/2007/12/10/alphabetical-asciibetical
|
115
|
+
* http://nedbatchelder.com/blog/200712.html#e20071211T054956
|
116
|
+
|
117
|
+
## License
|
118
|
+
|
119
|
+
Copyright (c) 2007 Benjamin Francisoud
|
120
|
+
|
121
|
+
Licensed under the MIT License. Refer to LICENSE for details.
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Public: The public interface for NaturalSort module
|
4
|
+
# For method descriptions refer to NaturalSort::Engine
|
5
|
+
#
|
6
|
+
# Examples
|
7
|
+
#
|
8
|
+
# require 'natural_sort'
|
9
|
+
# NaturalSort.sort ['a1', 'a12', 'a2'] #=> ['a1', 'a2', 'a12']
|
10
|
+
#
|
11
|
+
# # or
|
12
|
+
#
|
13
|
+
# require 'natural_sort_kernel'
|
14
|
+
# ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
|
15
|
+
module NaturalSort
|
16
|
+
|
17
|
+
class << self
|
18
|
+
extend Forwardable
|
19
|
+
|
20
|
+
# Public: sort
|
21
|
+
#
|
22
|
+
# Signature
|
23
|
+
#
|
24
|
+
# NaturalSort.sort(object)
|
25
|
+
#
|
26
|
+
# Refer to NaturalSort::Engine.sort
|
27
|
+
def_delegator :'NaturalSort::Engine', :sort
|
28
|
+
|
29
|
+
# Public: comparator
|
30
|
+
#
|
31
|
+
# Signature
|
32
|
+
#
|
33
|
+
# NaturalSort.comparator(a, b)
|
34
|
+
#
|
35
|
+
# Refer to NaturalSort::Engine.comparator
|
36
|
+
def_delegator :'NaturalSort::Engine', :comparator
|
37
|
+
|
38
|
+
# Deprecated: Alias to NaturalSort.sort. Was deprecated in version 1.2.0.
|
39
|
+
# To be removed in version 2.0.0 (according to semver)
|
40
|
+
#
|
41
|
+
# object - the object to sort, which must either be an Enumerable or implement #to_a to be meaningful.
|
42
|
+
#
|
43
|
+
# Returns a sorted version of the object.
|
44
|
+
def naturalsort(object)
|
45
|
+
warn('NaturalSort.naturalsort is deprecated and will be removed. Use NaturalSort.sort instead')
|
46
|
+
NaturalSort::Engine.sort(object)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Public: Main method to sort (other are just aliases). To be used when including
|
51
|
+
# NaturalSort into another object. Delegates to NaturalSort::Engine.sort
|
52
|
+
#
|
53
|
+
# Note that the object must either be an Enumerable or implement #to_a to be meaningful.
|
54
|
+
#
|
55
|
+
# Returns a sorted version of the object.
|
56
|
+
#
|
57
|
+
# Examples
|
58
|
+
#
|
59
|
+
# require 'natural_sort'
|
60
|
+
#
|
61
|
+
# class MyObject
|
62
|
+
# include NaturalSort
|
63
|
+
# def to_a
|
64
|
+
# ['a3, a2, a1']
|
65
|
+
# end
|
66
|
+
# end
|
67
|
+
def natural_sort
|
68
|
+
NaturalSort::Engine.sort(self)
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Internal: Singleton module which sorts elements in a natural,
|
4
|
+
# human-friendly alphanumeric order.
|
5
|
+
module NaturalSort::Engine
|
6
|
+
|
7
|
+
# Internal: Main Regexp used to in natural sorting
|
8
|
+
REGEXP = /(^|\D+)(\d+|(\D$))/
|
9
|
+
|
10
|
+
# Internal: A Regexp used to detect numeric substrings
|
11
|
+
NUMERIC = /(\d+)/
|
12
|
+
|
13
|
+
class << self
|
14
|
+
|
15
|
+
# Internal: Static method to sort.
|
16
|
+
#
|
17
|
+
# object - any object that is enumerable or has a #to_a method.
|
18
|
+
#
|
19
|
+
# Returns a sorted version of the object.
|
20
|
+
#
|
21
|
+
# Examples:
|
22
|
+
#
|
23
|
+
# NaturalSort.sort ['a1', 'a12', 'a2'] #=> ['a1', 'a2', 'a12']
|
24
|
+
def sort(object)
|
25
|
+
Array(object).sort do |a,b|
|
26
|
+
self.comparator(a,b)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Internal: Comparator function used for sorting, which can be
|
31
|
+
# used as a standalone.
|
32
|
+
#
|
33
|
+
# a - the left-hand side of the comparator
|
34
|
+
# b - the right-hand side of the comparator
|
35
|
+
#
|
36
|
+
# Returns 0, 1, or -1
|
37
|
+
#
|
38
|
+
# Examples
|
39
|
+
#
|
40
|
+
# [person1, person2, person3].sort{|a,b| NaturalSort.comparator(a.name, b.name)}
|
41
|
+
def comparator(a, b)
|
42
|
+
sa, sb = a.to_s, b.to_s
|
43
|
+
if (sa.downcase <=> sb.downcase) == 0 then sa <=> sb
|
44
|
+
else
|
45
|
+
na, nb = check_regexp(sa, sb)
|
46
|
+
na <=> nb
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def check_regexp(sa, sb)
|
53
|
+
ma, mb = multireg(REGEXP,sa), multireg(REGEXP,sb)
|
54
|
+
it = 0
|
55
|
+
equal = 0
|
56
|
+
ret = [sa, sb]
|
57
|
+
while (it < [ma.size,mb.size].min) and (equal==0)
|
58
|
+
if (ma[it] and mb[it]) and (ma[it][1] and mb[it][1]) and (NUMERIC.match ma[it][0] and NUMERIC.match mb[it][0])
|
59
|
+
l = [ma[it][2].size,mb[it][2].size].max
|
60
|
+
ret = [format(ma[it], l), format(mb[it], l)]
|
61
|
+
else
|
62
|
+
ret = [ma[it][0].downcase, mb[it][0].downcase]
|
63
|
+
end
|
64
|
+
equal = ret[0] <=> ret[1]
|
65
|
+
it+=1
|
66
|
+
end
|
67
|
+
return ret[0], ret[1]
|
68
|
+
end
|
69
|
+
|
70
|
+
# format([a, 1], 3) => a001
|
71
|
+
# add leading zero
|
72
|
+
def format(match_data, length)
|
73
|
+
match_data[1].gsub('_', '').downcase + ("%0#{length}d" % match_data[2].to_i)
|
74
|
+
end
|
75
|
+
|
76
|
+
# return an array with
|
77
|
+
# regexp matchdata on str
|
78
|
+
def multireg(regpexp, str)
|
79
|
+
result = []
|
80
|
+
while regpexp.match(str)
|
81
|
+
result.push regpexp.match(str)
|
82
|
+
str = regpexp.match(str).post_match
|
83
|
+
end
|
84
|
+
result
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Enumerable
|
4
|
+
# Add #natural_sort method to Enumerable module.
|
5
|
+
#
|
6
|
+
# Examples
|
7
|
+
#
|
8
|
+
# require 'natural_sort_kernel'
|
9
|
+
# ['a1', 'a12', 'a2'].natural_sort #=> ['a1', 'a2', 'a12']
|
10
|
+
# ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
|
11
|
+
include NaturalSort
|
12
|
+
end
|
13
|
+
|
14
|
+
class Array
|
15
|
+
# Add #natural_sort method to Array class.
|
16
|
+
#
|
17
|
+
# Examples
|
18
|
+
#
|
19
|
+
# require 'natural_sort_kernel'
|
20
|
+
# ['a1', 'a12', 'a2'].natural_sort #=> ['a1', 'a2', 'a12']
|
21
|
+
# ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
|
22
|
+
include NaturalSort
|
23
|
+
end
|
24
|
+
|
25
|
+
class Range
|
26
|
+
# Add #natural_sort method to Range class, which aliases #to_a
|
27
|
+
#
|
28
|
+
# Examples
|
29
|
+
#
|
30
|
+
# require 'natural_sort_kernel'
|
31
|
+
# (1..11).natural_sort #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
|
32
|
+
alias :natural_sort :to_a
|
33
|
+
end
|
34
|
+
|
35
|
+
if defined?(Set)
|
36
|
+
class Set
|
37
|
+
# Add #natural_sort method to Set class, if it has been initialized.
|
38
|
+
#
|
39
|
+
# Examples
|
40
|
+
#
|
41
|
+
# require 'set'
|
42
|
+
# require 'natural_sort_kernel'
|
43
|
+
# Set.new(['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D']).natural_sort #=> ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd']
|
44
|
+
include NaturalSort
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class Hash
|
49
|
+
# Add #natural_sort method to Hash class.
|
50
|
+
#
|
51
|
+
# Examples
|
52
|
+
#
|
53
|
+
# require 'natural_sort_kernel'
|
54
|
+
# { "a" => "value", "b" => "value", "A" => "value", "B" => "value" }.natural_sort #=>
|
55
|
+
# [["A", "value"], ["a", "value"], ["B", "value"], ["b", "value"]]
|
56
|
+
include NaturalSort
|
57
|
+
end
|
data/lib/natural_sort.rb
CHANGED
@@ -1,102 +1,6 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
#
|
8
|
-
# ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
|
9
|
-
module NaturalSort
|
10
|
-
VERSION = '1.1.1'
|
11
|
-
|
12
|
-
# call-seq:
|
13
|
-
# NaturalSort::naturalsort(object) => array
|
14
|
-
#
|
15
|
-
# Static method to sort.
|
16
|
-
#
|
17
|
-
# *Usage*
|
18
|
-
# NaturalSort::naturalsort ['a1', 'a12', 'a2'] #=> ['a1', 'a2', 'a12']
|
19
|
-
#
|
20
|
-
# <tt>object</tt> can by any object that has to_a method.
|
21
|
-
def self.naturalsort(object)
|
22
|
-
sorted = object.to_a.sort do |a,b|
|
23
|
-
sa, sb = a.to_s, b.to_s
|
24
|
-
if ((sa.downcase <=> sb.downcase) == 0) then sa <=> sb
|
25
|
-
else
|
26
|
-
na, nb = check_regexp(sa, sb)
|
27
|
-
na <=> nb
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# call-seq:
|
33
|
-
# object.natural_sort => array
|
34
|
-
#
|
35
|
-
# Main method to sort (other are just aliases).
|
36
|
-
#
|
37
|
-
# *Usage*
|
38
|
-
# require 'natural_sort'
|
39
|
-
# include NaturalSort
|
40
|
-
#
|
41
|
-
# object.natural_sort #=> ['a1', 'a2', 'a12']
|
42
|
-
#
|
43
|
-
# <tt>object</tt> can by any object that has a method <tt>to_a</tt>
|
44
|
-
#
|
45
|
-
# See <tt>natural_sort_kernel.rb</tt> to add natural sort methods to default ruby objects.
|
46
|
-
# Enumerable , Array, Range, Set, Hash
|
47
|
-
def natural_sort
|
48
|
-
NaturalSort::naturalsort(to_a)
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def self.check_regexp(sa, sb)
|
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
|
70
|
-
end
|
71
|
-
return ret[0], ret[1]
|
72
|
-
end
|
73
|
-
|
74
|
-
def check_regexp(sa, sb)
|
75
|
-
NaturalSort::check_regexp(sa, sb)
|
76
|
-
end
|
77
|
-
|
78
|
-
# format([a, 1], 3) => a001
|
79
|
-
# add leading zero
|
80
|
-
def self.format(match_data, length)
|
81
|
-
match_data[1].gsub("_", "").downcase + ("%0#{length}d" % match_data[2].to_i)
|
82
|
-
end
|
83
|
-
|
84
|
-
def format(match_data, length)
|
85
|
-
NaturalSort::format(match_data, length)
|
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
|
102
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
require 'natural_sort/version'
|
5
|
+
require 'natural_sort/engine'
|
6
|
+
require 'natural_sort/base'
|
data/lib/natural_sort_kernel.rb
CHANGED
@@ -1,47 +1,4 @@
|
|
1
|
-
#
|
2
|
-
require File.dirname(__FILE__) + '/natural_sort.rb'
|
3
|
-
require 'set'
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
#
|
8
|
-
# ['a1', 'a12', 'a2'].alphanum_sort #=> ['a1', 'a2', 'a12']
|
9
|
-
# ['a', 'b', 'A', 'B'].alphanum_sort #=> ['A', 'a', 'B', 'b']
|
10
|
-
module Enumerable
|
11
|
-
include NaturalSort
|
12
|
-
end
|
13
|
-
|
14
|
-
# Add NaturalSort methods.
|
15
|
-
# require 'natural_sort_kernel'
|
16
|
-
#
|
17
|
-
# ['a1', 'a12', 'a2'].alphanum_sort #=> ['a1', 'a2', 'a12']
|
18
|
-
# ['a', 'b', 'A', 'B'].alphanum_sort #=> ['A', 'a', 'B', 'b']
|
19
|
-
class Array
|
20
|
-
include NaturalSort
|
21
|
-
end
|
22
|
-
|
23
|
-
# Add NaturalSort methods.
|
24
|
-
# require 'natural_sort_kernel'
|
25
|
-
#
|
26
|
-
# (1..21).alphanum_sort #=> [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9]
|
27
|
-
class Range
|
28
|
-
include NaturalSort
|
29
|
-
end
|
30
|
-
|
31
|
-
# Add NaturalSort methods.
|
32
|
-
# require 'set'
|
33
|
-
# require 'natural_sort_kernel'
|
34
|
-
#
|
35
|
-
# Set.new(['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D']).alphanum_sort #=> ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd']
|
36
|
-
class Set
|
37
|
-
include NaturalSort
|
38
|
-
end
|
39
|
-
|
40
|
-
# Add NaturalSort methods.
|
41
|
-
# require 'natural_sort_kernel'
|
42
|
-
#
|
43
|
-
# { "a" => "value", "b" => "value", "c" => "value", "d" => "value", "A" => "value", "B" => "value", "C" => "value", "D" => "value" }.alphanum_sort
|
44
|
-
# => [["A", "value"], ["a", "value"], ["B", "value"], ["b", "value"], ["C", "value"], ["c", "value"], ["D", "value"], ["d", "value"]]
|
45
|
-
class Hash
|
46
|
-
include NaturalSort
|
47
|
-
end
|
3
|
+
require 'natural_sort'
|
4
|
+
require 'natural_sort/kernel'
|
metadata
CHANGED
@@ -1,121 +1,81 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: naturalsort
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 1
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
version: 1.1.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.3.0
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
12
|
-
-
|
13
|
-
autorequire:
|
6
|
+
authors:
|
7
|
+
- Johnny Shields
|
8
|
+
autorequire:
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
requirements:
|
11
|
+
date: 2024-10-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
25
17
|
- - ">="
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
|
28
|
-
- 2
|
29
|
-
- 0
|
30
|
-
- 4
|
31
|
-
version: 2.0.4
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
32
20
|
type: :development
|
33
|
-
version_requirements: *id001
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: hoe
|
36
21
|
prerelease: false
|
37
|
-
|
38
|
-
requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
39
31
|
- - ">="
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
|
42
|
-
- 2
|
43
|
-
- 6
|
44
|
-
- 0
|
45
|
-
version: 2.6.0
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
46
34
|
type: :development
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: 'Example: %w[1 2a A1 a11 A12 a2 a21 x__2 X_1].natural_sort => %w[1 2a
|
42
|
+
A1 a11 A12 a2 a21 x__2 X_1]'
|
43
|
+
email: info@tablecheck.com
|
67
44
|
executables: []
|
68
|
-
|
69
45
|
extensions: []
|
70
|
-
|
71
|
-
|
72
|
-
-
|
73
|
-
-
|
74
|
-
- README.
|
75
|
-
files:
|
76
|
-
- History.txt
|
77
|
-
- Manifest.txt
|
78
|
-
- README.txt
|
79
|
-
- Rakefile
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- CHANGELOG.md
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
80
51
|
- lib/natural_sort.rb
|
52
|
+
- lib/natural_sort/base.rb
|
53
|
+
- lib/natural_sort/engine.rb
|
54
|
+
- lib/natural_sort/kernel.rb
|
55
|
+
- lib/natural_sort/version.rb
|
81
56
|
- lib/natural_sort_kernel.rb
|
82
|
-
|
83
|
-
|
84
|
-
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
post_install_message:
|
91
|
-
rdoc_options:
|
92
|
-
- --main
|
93
|
-
- README.txt
|
94
|
-
require_paths:
|
57
|
+
homepage: https://github.com/johnnyshields/naturalsort
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
95
64
|
- lib
|
96
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
-
requirements:
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
98
67
|
- - ">="
|
99
|
-
- !ruby/object:Gem::Version
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
-
requirements:
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
105
72
|
- - ">="
|
106
|
-
- !ruby/object:Gem::Version
|
107
|
-
|
108
|
-
- 0
|
109
|
-
version: "0"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
110
75
|
requirements: []
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
76
|
+
rubygems_version: 3.5.11
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: NaturalSort is a simple library which implements a natural, human-friendly
|
80
|
+
alphanumeric sort in Ruby
|
81
|
+
test_files: []
|
data/History.txt
DELETED
@@ -1,24 +0,0 @@
|
|
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
|
-
|
13
|
-
== 1.1.0 / 2008-01-08
|
14
|
-
* Improve documentation to get better google results
|
15
|
-
* Remove alias methods: <tt>sort_natural, sort_alpha, alpha_sort, sort_alphabetical, alphabetical_sort, sort_alphanum, alphanum_sort</tt>
|
16
|
-
|
17
|
-
== 1.0.1 / 2007-12-18
|
18
|
-
|
19
|
-
* Fix problem using as a static method
|
20
|
-
* Refactoring code
|
21
|
-
|
22
|
-
== 1.0.0 / 2007-12-14
|
23
|
-
|
24
|
-
* Project creation
|
data/Manifest.txt
DELETED
data/README.txt
DELETED
@@ -1,87 +0,0 @@
|
|
1
|
-
NaturalSort
|
2
|
-
http://naturalsort.rubyforge.org/
|
3
|
-
|
4
|
-
== Description:
|
5
|
-
|
6
|
-
NaturalSort is a small and simple library to implements a natural or human alphabetical sort in ruby.
|
7
|
-
It's sometimes call:
|
8
|
-
* natural sort
|
9
|
-
* alpha sort
|
10
|
-
* alphabetical sort
|
11
|
-
* alphanum sort
|
12
|
-
|
13
|
-
Examples:
|
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']
|
19
|
-
|
20
|
-
== Features:
|
21
|
-
|
22
|
-
* sort case insensitive
|
23
|
-
* sort filename matching pattern "abc1", "abc12", "abc2" in the correct order
|
24
|
-
* sort filename matching pattern "a1b2"
|
25
|
-
* sort underscore insensitive
|
26
|
-
|
27
|
-
== Install:
|
28
|
-
|
29
|
-
* sudo gem install naturalsort
|
30
|
-
|
31
|
-
== Synopsis:
|
32
|
-
|
33
|
-
=== Usage N�1 - Add to your ruby default objects
|
34
|
-
Add natural sort methods to ruby default object (Array, Hash, etc...)
|
35
|
-
|
36
|
-
require 'natural_sort_kernel'
|
37
|
-
|
38
|
-
sorted = ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D'].natural_sort
|
39
|
-
...
|
40
|
-
|
41
|
-
=== Usage N�2 - Use only one method
|
42
|
-
|
43
|
-
require 'natural_sort'
|
44
|
-
|
45
|
-
sorted = NaturalSort::naturalsort ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D']
|
46
|
-
|
47
|
-
== About
|
48
|
-
|
49
|
-
* Rubyforge project page http://rubyforge.org/projects/naturalsort
|
50
|
-
* Author: Benjamin Francisoud http://www.google.com/profiles/benjamin.francisoud
|
51
|
-
|
52
|
-
== Related Links
|
53
|
-
|
54
|
-
Links related to the alphabetical sorting problem:
|
55
|
-
* http://www.codinghorror.com/blog/archives/001018.html
|
56
|
-
* http://sourcefrog.net/projects/natsort/
|
57
|
-
* http://be2.php.net/natsort
|
58
|
-
* http://www.wellho.net/forum/Programming-in-Python-and-Ruby/Python-Sort-Order.html
|
59
|
-
* http://www.davekoelle.com/alphanum.html
|
60
|
-
* http://rikkus.info/arch/sensible_sort.rb
|
61
|
-
* http://weblog.masukomi.org/2007/12/10/alphabetical-asciibetical
|
62
|
-
* http://nedbatchelder.com/blog/200712.html#e20071211T054956
|
63
|
-
|
64
|
-
== License:
|
65
|
-
|
66
|
-
(The MIT License)
|
67
|
-
|
68
|
-
Copyright (c) 2007 Benjamin Francisoud
|
69
|
-
|
70
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
71
|
-
a copy of this software and associated documentation files (the
|
72
|
-
'Software'), to deal in the Software without restriction, including
|
73
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
74
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
75
|
-
permit persons to whom the Software is furnished to do so, subject to
|
76
|
-
the following conditions:
|
77
|
-
|
78
|
-
The above copyright notice and this permission notice shall be
|
79
|
-
included in all copies or substantial portions of the Software.
|
80
|
-
|
81
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
82
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
83
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
84
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
85
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
86
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
87
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
# -*- ruby -*-
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'hoe'
|
5
|
-
require './lib/natural_sort.rb'
|
6
|
-
|
7
|
-
Hoe.new('naturalsort', NaturalSort::VERSION) do |p|
|
8
|
-
p.rubyforge_name = 'naturalsort'
|
9
|
-
p.author = 'Benjamin Francisoud'
|
10
|
-
p.email = 'pub.cog@gmail.com'
|
11
|
-
p.summary = 'NaturalSort is a small and simple library to implements a natural or human friendly alphabetical sort in ruby.'
|
12
|
-
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
13
|
-
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
14
|
-
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
15
|
-
p.remote_rdoc_dir = '' # Release to root
|
16
|
-
p.need_zip = true
|
17
|
-
end
|
18
|
-
|
19
|
-
SVN = 'svn+ssh://cogito@rubyforge.org/var/svn/naturalsort'
|
20
|
-
|
21
|
-
task :svn_branch => [:clean] do
|
22
|
-
`svn delete #{SVN}/branches/1.X -m "cleanup branch"`
|
23
|
-
`svn copy #{SVN}/trunk #{SVN}/branches/1.X -m "create branch"`
|
24
|
-
end
|
25
|
-
|
26
|
-
task :svn_tag do
|
27
|
-
`svn copy #{SVN}/trunk #{SVN}/tags/#{NaturalSort::VERSION} -m "create tag"`
|
28
|
-
end
|
29
|
-
|
30
|
-
# vim: syntax=Ruby
|
data/test/test_helper.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
|
3
|
-
module NaturalSort
|
4
|
-
Base = ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D']
|
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']
|
29
|
-
end
|
data/test/test_natural_sort.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
-
require File.dirname(__FILE__) + '/../lib/natural_sort.rb'
|
3
|
-
|
4
|
-
class MyClass
|
5
|
-
include NaturalSort
|
6
|
-
|
7
|
-
def initialize(array = Base)
|
8
|
-
@array = array
|
9
|
-
end
|
10
|
-
|
11
|
-
def to_a
|
12
|
-
@array
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
# Test without include
|
17
|
-
class TestNaturalSortSelf < Test::Unit::TestCase
|
18
|
-
def test_self
|
19
|
-
assert_equal NaturalSort::BaseSorted, NaturalSort::naturalsort(NaturalSort::Base)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
# Test using include
|
24
|
-
class TestNaturalSort < Test::Unit::TestCase
|
25
|
-
include NaturalSort
|
26
|
-
|
27
|
-
def setup
|
28
|
-
@obj = MyClass.new
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_case_sensitive
|
32
|
-
sorted = @obj.natural_sort
|
33
|
-
assert_equal BaseSorted, sorted
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_mixed
|
37
|
-
obj = MyClass.new ['a1', 'a12', 'A11', 'a2', 'a10', 'A3', 'a21', 'A29']
|
38
|
-
assert_equal ['a1', 'a2', 'A3', 'a10', 'A11', 'a12', 'a21', 'A29'], obj.natural_sort
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_numbers
|
42
|
-
obj = MyClass.new ['a1', 'a12', 'a11', 'a2', 'a10', 'a3', 'a21', 'a29']
|
43
|
-
assert_equal ['a1', 'a2', 'a3', 'a10', 'a11', 'a12', 'a21', 'a29'], obj.natural_sort
|
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
|
55
|
-
end
|
@@ -1,9 +0,0 @@
|
|
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
|
@@ -1,109 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
-
require File.dirname(__FILE__) + '/../lib/natural_sort_kernel.rb'
|
3
|
-
|
4
|
-
class TestEnum
|
5
|
-
include Enumerable
|
6
|
-
def to_a
|
7
|
-
NaturalSort::Base
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class TestNaturalSortKernel < Test::Unit::TestCase
|
12
|
-
|
13
|
-
def test_empty
|
14
|
-
assert_equal(['', ''], ['', ''].natural_sort)
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_enum
|
18
|
-
enum = TestEnum.new
|
19
|
-
assert_equal NaturalSort::BaseSorted, enum.natural_sort
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_array
|
23
|
-
assert_equal NaturalSort::BaseSorted, NaturalSort::Base.natural_sort
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_range
|
27
|
-
expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
|
28
|
-
assert_equal expected, (1..21).natural_sort
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_set
|
32
|
-
set = Set.new NaturalSort::Base
|
33
|
-
assert_equal NaturalSort::BaseSorted, set.natural_sort
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_hash
|
37
|
-
expected = [["A", "value"], ["a", "value"], ["B", "value"], ["b", "value"],
|
38
|
-
["C", "value"], ["c", "value"], ["D", "value"], ["d", "value"]]
|
39
|
-
hash = { "a" => "value", "b" => "value", "c" => "value", "d" => "value", "A" => "value", "B" => "value", "C" => "value", "D" => "value" }
|
40
|
-
assert_equal expected, hash.natural_sort
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_identical_simple
|
44
|
-
assert_equal(['x', 'x'], ['x', 'x'].natural_sort)
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_identical_two_groups
|
48
|
-
assert_equal(['x1', 'x1'], ['x1', 'x1'].natural_sort)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_ordered_simple
|
52
|
-
assert_equal(['x', 'y'], ['x', 'y'].natural_sort)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_ordered_simple_start_backwards
|
56
|
-
assert_equal(['x', 'y'], ['y', 'x'].natural_sort)
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_ordered_two_groups
|
60
|
-
assert_equal(['x1', 'x2'], ['x1', 'x2'].natural_sort)
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_ordered_two_groups_start_backwards
|
64
|
-
assert_equal(['x1', 'x2'], ['x2', 'x1'].natural_sort)
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_ordered_two_groups_separated
|
68
|
-
assert_equal(['x_1', 'x_2'], ['x_2', 'x_1'].natural_sort)
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_ordered_two_groups_separated_different_distances
|
72
|
-
assert_equal(['x_1', 'x__2'], ['x__2', 'x_1'].natural_sort)
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_ordered_two_groups_separated_different_distances_swapped
|
76
|
-
assert_equal(['x__1', 'x_2'], ['x_2', 'x__1'].natural_sort)
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_three_groups
|
80
|
-
assert_equal(
|
81
|
-
['hello 2 world', 'hello world', 'hello world 2'],
|
82
|
-
['hello world', 'hello world 2', 'hello 2 world'].natural_sort
|
83
|
-
)
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_decimal
|
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 this test doesn't pass and need to be fix"
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_multiple_string_number
|
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
|
94
|
-
end
|
95
|
-
|
96
|
-
# same as test_multiple_string_number but first number has (sometimes) leading zero
|
97
|
-
def test_multiple_string_number_2
|
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
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_filename
|
103
|
-
assert_equal ["img1.png", "img2.png", "img10.png", "img12.png"], ["img12.png", "img10.png", "img2.png", "img1.png"].natural_sort
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_complex
|
107
|
-
assert_equal NaturalSort::ComplexSorted, NaturalSort::Complex.natural_sort
|
108
|
-
end
|
109
|
-
end
|