naturalsort 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ == 1.0.0 / 2007-12-14
2
+
3
+ * Project creation
data/Manifest.txt ADDED
@@ -0,0 +1,9 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/natural_sort.rb
6
+ lib/natural_sort_kernel.rb
7
+ test/test_helper.rb
8
+ test/test_natural_sort.rb
9
+ test/test_natural_sort_kernel.rb
data/README.txt ADDED
@@ -0,0 +1,79 @@
1
+ NaturalSort
2
+ * Rubyforge Project http://rubyforge.org/projects/naturalsort
3
+ * by Benjamin Francisoud http://benjamin.francisoud.googlepages.com
4
+
5
+ == Description:
6
+
7
+ NaturalSort is a small and simple library to implements a natural or human alphabetical sort in ruby.
8
+
9
+ Examples:
10
+ ['a1', 'a11', 'a12', 'a2', 'a21'] => ['a1', 'a2', 'a11', 'a12','a21']
11
+ ['a', 'b', 'c', 'A', 'B', 'C'] => ['A', 'a', 'B', 'b', 'C', 'c']
12
+ ['x__2', 'x_1'] => ['x_1', 'x__2']
13
+
14
+ == Features:
15
+
16
+ * sort case insensitive
17
+ * sort filename matching pattern "abc1", "abc12", "abc2" in the correct order
18
+ * sort underscore insensitive
19
+
20
+ == Synopsis:
21
+
22
+ === Usage N�1 - Add to your ruby default objects
23
+ Add natural sort methods to ruby default object (Array, Hash, etc...)
24
+
25
+ require 'natural_sort_kernel'
26
+
27
+ sorted = ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D'].alphanum_sort
28
+ sorted = ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D'].alpha_sort
29
+ sorted = ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D'].natural_sort
30
+ sorted = ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D'].sort_natural
31
+ ...
32
+ Pick your favorite method name: NaturalSort
33
+
34
+ === Usage N�2 - Use only one method
35
+
36
+ require 'natural_sort'
37
+
38
+ sorted = NaturalSort::naturalsort ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D']
39
+
40
+ == Install:
41
+
42
+ * sudo gem install natural_sort
43
+
44
+ == Related Links
45
+
46
+ Links related to the alphabetical sorting problem:
47
+ * http://www.codinghorror.com/blog/archives/001018.html
48
+ * http://sourcefrog.net/projects/natsort/
49
+ * http://be2.php.net/natsort
50
+ * http://www.wellho.net/forum/Programming-in-Python-and-Ruby/Python-Sort-Order.html
51
+ * http://www.davekoelle.com/alphanum.html
52
+ * http://rikkus.info/arch/sensible_sort.rb
53
+ * http://weblog.masukomi.org/2007/12/10/alphabetical-asciibetical
54
+ * http://nedbatchelder.com/blog/200712.html
55
+
56
+ == License:
57
+
58
+ (The MIT License)
59
+
60
+ Copyright (c) 2007 Benjamin Francisoud
61
+
62
+ Permission is hereby granted, free of charge, to any person obtaining
63
+ a copy of this software and associated documentation files (the
64
+ 'Software'), to deal in the Software without restriction, including
65
+ without limitation the rights to use, copy, modify, merge, publish,
66
+ distribute, sublicense, and/or sell copies of the Software, and to
67
+ permit persons to whom the Software is furnished to do so, subject to
68
+ the following conditions:
69
+
70
+ The above copyright notice and this permission notice shall be
71
+ included in all copies or substantial portions of the Software.
72
+
73
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
74
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
75
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
76
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
77
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
78
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
79
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
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
+ # vim: syntax=Ruby
@@ -0,0 +1,73 @@
1
+ # Sort elements in a natural / alphabetical / human friendly order.
2
+ # require 'natural_sort'
3
+ #
4
+ # NaturalSort::naturalsort ['a1', 'a12', 'a2'] #=> ['a1', 'a2', 'a12']
5
+ # or
6
+ # require 'natural_sort_kernel'
7
+ #
8
+ # ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
9
+ module NaturalSort
10
+ VERSION = '1.0.0'
11
+
12
+ # call-seq:
13
+ # NaturalSort::naturalsort(object) => array
14
+ #
15
+ # Static method to sort.
16
+ #
17
+ # *Usage*
18
+ # naturalsort ['a1', 'a12', 'a2'] #=> ['a1', 'a2', 'a12']
19
+ #
20
+ # <tt>object</tt> can by any object that has to_a and sort methods.
21
+ def self.naturalsort(object)
22
+ object.natural_sort
23
+ end
24
+
25
+ # call-seq:
26
+ # object.natural_sort => array
27
+ #
28
+ # Main method to sort (other are just aliases).
29
+ #
30
+ # *Usage*
31
+ # require 'natural_sort'
32
+ # include NaturalSort
33
+ #
34
+ # object.natural_sort #=> ['a1', 'a2', 'a12']
35
+ #
36
+ # <tt>object</tt> can by any object that has a method <tt>to_a</tt>
37
+ #
38
+ # See <tt>natural_sort_kernel.rb</tt> to add natural sort methods to default ruby objects.
39
+ # Enumerable , Array, Range, Set, Hash
40
+ def natural_sort
41
+ sorted = to_a.sort do |a,b|
42
+ sa, sb = a.to_s, b.to_s
43
+ if ((sa.downcase <=> sb.downcase) == 0)
44
+ sa <=> sb
45
+ else
46
+ regexp = /(\D+)(\d+)/
47
+ ma, mb = regexp.match(sa), regexp.match(sb)
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
55
+ end
56
+ end
57
+ end
58
+
59
+ alias sort_natural natural_sort
60
+ alias sort_alpha natural_sort
61
+ alias alpha_sort natural_sort
62
+ alias sort_alphabetical natural_sort
63
+ alias alphabetical_sort natural_sort
64
+ alias sort_alphanum natural_sort
65
+ alias alphanum_sort natural_sort
66
+
67
+ private
68
+ # format([a, 1], 3) => a001
69
+ # add leading zero
70
+ def format(match_data, length)
71
+ match_data[1].gsub("_", "").downcase + ("%0#{length}d" % match_data[2])
72
+ end
73
+ end
@@ -0,0 +1,47 @@
1
+ # Add natural sorting method to default Kernel ruby objects.
2
+ require File.dirname(__FILE__) + '/natural_sort.rb'
3
+ require 'set'
4
+
5
+ # Add NaturalSort methods.
6
+ # require 'natural_sort_kernel'
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
@@ -0,0 +1,6 @@
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
+ end
@@ -0,0 +1,53 @@
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_alias
32
+ assert_equal BaseSorted, (@obj.sort_natural)
33
+ assert_equal BaseSorted, (@obj.sort_alpha)
34
+ assert_equal BaseSorted, (@obj.alpha_sort)
35
+ assert_equal BaseSorted, (@obj.sort_alphabetical)
36
+ assert_equal BaseSorted, (@obj.alphabetical_sort)
37
+ end
38
+
39
+ def test_case_sensitive
40
+ sorted = @obj.natural_sort
41
+ assert_equal BaseSorted, sorted
42
+ end
43
+
44
+ def test_mixed
45
+ obj = MyClass.new ['a1', 'a12', 'A11', 'a2', 'a10', 'A3', 'a21', 'A29']
46
+ assert_equal ['a1', 'a2', 'A3', 'a10', 'A11', 'a12', 'a21', 'A29'], obj.natural_sort
47
+ end
48
+
49
+ def test_numbers
50
+ obj = MyClass.new ['a1', 'a12', 'a11', 'a2', 'a10', 'a3', 'a21', 'a29']
51
+ assert_equal ['a1', 'a2', 'a3', 'a10', 'a11', 'a12', 'a21', 'a29'], obj.natural_sort
52
+ end
53
+ end
@@ -0,0 +1,87 @@
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.alphanum_sort
20
+ end
21
+
22
+ def test_array
23
+ assert_equal NaturalSort::BaseSorted, NaturalSort::Base.alphanum_sort
24
+ assert_equal NaturalSort::BaseSorted, NaturalSort::Base.alpha_sort
25
+ assert_equal NaturalSort::BaseSorted, NaturalSort::Base.sort_natural
26
+ end
27
+
28
+ def test_range
29
+ expected = [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9]
30
+ assert_equal expected, (1..21).alphanum_sort
31
+ end
32
+
33
+ def test_set
34
+ set = Set.new NaturalSort::Base
35
+ assert_equal NaturalSort::BaseSorted, set.alphanum_sort
36
+ end
37
+
38
+ def test_hash
39
+ expected = [["A", "value"], ["a", "value"], ["B", "value"], ["b", "value"],
40
+ ["C", "value"], ["c", "value"], ["D", "value"], ["d", "value"]]
41
+ hash = { "a" => "value", "b" => "value", "c" => "value", "d" => "value", "A" => "value", "B" => "value", "C" => "value", "D" => "value" }
42
+ assert_equal expected, hash.alphanum_sort
43
+ end
44
+
45
+ def test_identical_simple
46
+ assert_equal(['x', 'x'], ['x', 'x'].natural_sort)
47
+ end
48
+
49
+ def test_identical_two_groups
50
+ assert_equal(['x1', 'x1'], ['x1', 'x1'].natural_sort)
51
+ end
52
+
53
+ def test_ordered_simple
54
+ assert_equal(['x', 'y'], ['x', 'y'].natural_sort)
55
+ end
56
+
57
+ def test_ordered_simple_start_backwards
58
+ assert_equal(['x', 'y'], ['y', 'x'].natural_sort)
59
+ end
60
+
61
+ def test_ordered_two_groups
62
+ assert_equal(['x1', 'x2'], ['x1', 'x2'].natural_sort)
63
+ end
64
+
65
+ def test_ordered_two_groups_start_backwards
66
+ assert_equal(['x1', 'x2'], ['x2', 'x1'].natural_sort)
67
+ end
68
+
69
+ def test_ordered_two_groups_separated
70
+ assert_equal(['x_1', 'x_2'], ['x_2', 'x_1'].natural_sort)
71
+ end
72
+
73
+ def test_ordered_two_groups_separated_different_distances
74
+ assert_equal(['x_1', 'x__2'], ['x__2', 'x_1'].natural_sort)
75
+ end
76
+
77
+ def test_ordered_two_groups_separated_different_distances_swapped
78
+ assert_equal(['x__1', 'x_2'], ['x_2', 'x__1'].natural_sort)
79
+ end
80
+
81
+ def test_three_groups
82
+ assert_equal(
83
+ ['hello 2 world', 'hello world', 'hello world 2'],
84
+ ['hello world', 'hello world 2', 'hello 2 world'].natural_sort
85
+ )
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: naturalsort
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2007-12-17 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: "* Rubyforge Project http://rubyforge.org/projects/naturalsort"
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 == Synopsis: === Usage N\xB01 - Add to your ruby default objects Add natural sort methods to ruby default object (Array, Hash, etc...)"
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:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Benjamin Francisoud
31
+ files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile
36
+ - lib/natural_sort.rb
37
+ - lib/natural_sort_kernel.rb
38
+ - test/test_helper.rb
39
+ - test/test_natural_sort.rb
40
+ - test/test_natural_sort_kernel.rb
41
+ test_files:
42
+ - test/test_helper.rb
43
+ - test/test_natural_sort.rb
44
+ - test/test_natural_sort_kernel.rb
45
+ rdoc_options:
46
+ - --main
47
+ - README.txt
48
+ extra_rdoc_files:
49
+ - History.txt
50
+ - Manifest.txt
51
+ - README.txt
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ requirements: []
57
+
58
+ dependencies:
59
+ - !ruby/object:Gem::Dependency
60
+ name: hoe
61
+ version_requirement:
62
+ version_requirements: !ruby/object:Gem::Version::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 1.3.0
67
+ version: