natural_sort 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4eaf8c2b84481c81c04561961d96b2ae8691c06a
4
+ data.tar.gz: cf25e73d6eeb71dc9e1072c724ea814dd524e277
5
+ SHA512:
6
+ metadata.gz: 8b0d846ad70d7bcafe66a32eda7f8b4be406288967ce34493eadc0591271ba8af1e9200caa9b3be0d7ebdf614693716b74baec7c9f7b3fefc989fcb307e64ccc
7
+ data.tar.gz: 7a48ce33aca18bbd35e3cc86d99156cc6af615200719f975ced0557fc8da5fe99283d66035318f3c4e23eff7281c61d0f7cae05084de1b4d11656d4006f6ce6e
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Pavel Pravosud
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Natural Sort
2
+
3
+ Natual sorting implementation in Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "natural_sort"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install natural_sort
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "natual_sort"
25
+
26
+ list = ["a10", "a", "a20", "a1b", "a1a", "a2", "a0", "a1"]
27
+ list.sort(&NatualSort) # => ["a", "a0", "a1", "a1a", "a1b", "a2", "a10", "a20"]
28
+ ```
29
+
30
+ ```ruby
31
+ UbuntuRelease = Struct.new(:number, :name)
32
+
33
+ ubuntu_releases = [
34
+ UbuntuRelease.new("9.04", "Jaunty Jackalope"),
35
+ UbuntuRelease.new("10.10", "Maverick Meerkat"),
36
+ UbuntuRelease.new("8.10", "Intrepid Ibex"),
37
+ UbuntuRelease.new("10.04.4", "Lucid Lynx"),
38
+ UbuntuRelease.new("9.10", "Karmic Koala"),
39
+ ]
40
+
41
+ ubuntu_releases.sort_by { |v| NaturalSort(v.number) }
42
+ # => [
43
+ # UbuntuRelease.new("8.10", "Intrepid Ibex"),
44
+ # UbuntuRelease.new("9.04", "Jaunty Jackalope"),
45
+ # UbuntuRelease.new("9.10", "Karmic Koala"),
46
+ # UbuntuRelease.new("10.04.4", "Lucid Lynx"),
47
+ # UbuntuRelease.new("10.10", "Maverick Meerkat")
48
+ # ]
49
+ ```
50
+
51
+ ## Refinements
52
+
53
+ If you're running ruby 2.1 or newer, you can use refinements.
54
+
55
+ ```ruby
56
+ require "natural_sort/refinments"
57
+
58
+ class MyClass
59
+ using NatualSort
60
+
61
+ list.natural_sort # => ["a", "a0", "a1", "a1a"...
62
+ ubuntu_releases.natual_sort_by(&:number) # => [ UbuntuRelease.new("8.10"...
63
+ end
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at
69
+ https://github.com/rwz/natural_sort.
70
+
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT
75
+ License](http://opensource.org/licenses/MIT).
76
+
@@ -0,0 +1,24 @@
1
+ require "natural_sort/version"
2
+
3
+ module NaturalSort
4
+ module_function
5
+
6
+ autoload :Segment, "natural_sort/segment"
7
+ autoload :SegmentedString, "natural_sort/segmented_string"
8
+
9
+ def to_proc
10
+ lambda(&method(:compare))
11
+ end
12
+
13
+ def sort(input)
14
+ input.sort(&self)
15
+ end
16
+
17
+ def compare(a, b)
18
+ SegmentedString.new(a) <=> SegmentedString.new(b)
19
+ end
20
+ end
21
+
22
+ def NaturalSort(input)
23
+ NaturalSort::SegmentedString.new(input)
24
+ end
@@ -0,0 +1,17 @@
1
+ require "set"
2
+
3
+ module NaturalSort
4
+ [Array, Hash, Set].each do |klass|
5
+ refine klass do
6
+ def natural_sort
7
+ to_a.sort(&NaturalSort)
8
+ end
9
+
10
+ def natural_sort_by
11
+ to_a.sort_by do |element|
12
+ NaturalSort(yield(element))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,36 @@
1
+ module NaturalSort
2
+ class Segment
3
+ include Comparable
4
+
5
+ NUMERIC = /\A\d+(?:\.\d+)?\z/
6
+ private_constant :NUMERIC
7
+
8
+ attr_reader :input
9
+
10
+ def initialize(input)
11
+ @input = input.to_s
12
+ end
13
+
14
+ def to_s
15
+ @input
16
+ end
17
+
18
+ def <=>(other)
19
+ if numeric? && other.numeric?
20
+ Rational(input) <=> Rational(other.to_s)
21
+ else
22
+ compare_chars(input, other.to_s)
23
+ end
24
+ end
25
+
26
+ def numeric?
27
+ NUMERIC === input
28
+ end
29
+
30
+ private
31
+
32
+ def compare_chars(a, b)
33
+ a == b.swapcase ? a <=> b : a.downcase <=> b.downcase
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,43 @@
1
+ module NaturalSort
2
+ class SegmentedString
3
+ include Comparable
4
+
5
+ TOKENIZER = /\d+(?:\.\d+)?|\D/
6
+ private_constant :TOKENIZER
7
+
8
+ attr_reader :input
9
+
10
+ def initialize(input)
11
+ @input = input.to_s
12
+ end
13
+
14
+ def segments
15
+ @segments ||= tokens.map { |token| Segment.new(token) }
16
+ end
17
+
18
+ def <=>(other)
19
+ raise ArgumentError unless SegmentedString === other
20
+
21
+ other_segments = other.segments
22
+
23
+ segments.each_with_index do |segment, index|
24
+ other_segment = other_segments[index]
25
+ return 1 if other_segment.nil?
26
+ result = compare_segments(segment, other_segment)
27
+ return result unless result.zero?
28
+ end
29
+
30
+ other_segments.length > segments.length ? -1 : 0
31
+ end
32
+
33
+ private
34
+
35
+ def tokens
36
+ @tokens ||= input.scan(TOKENIZER)
37
+ end
38
+
39
+ def compare_segments(segment, other)
40
+ segment <=> other
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module NaturalSort
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: natural_sort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Pravosud
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - pavel@pravosud.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - lib/natural_sort.rb
23
+ - lib/natural_sort/refinements.rb
24
+ - lib/natural_sort/segment.rb
25
+ - lib/natural_sort/segmented_string.rb
26
+ - lib/natural_sort/version.rb
27
+ homepage: https://github.com/rwz/natural_sort
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.5.1
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Natural sorting support for Ruby
51
+ test_files: []