naturally 1.0.1 → 1.0.2
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.
- data/lib/naturally.rb +7 -6
- data/lib/naturally/version.rb +1 -1
- metadata +1 -1
data/lib/naturally.rb
CHANGED
@@ -2,19 +2,20 @@ require "naturally/version"
|
|
2
2
|
|
3
3
|
module Naturally
|
4
4
|
|
5
|
-
#
|
6
|
-
#
|
5
|
+
# Perform a natural sort.
|
6
|
+
# @param [Array<String>] a list of numbers to sort.
|
7
|
+
# @return [Array<String>] the numbers sorted naturally.
|
7
8
|
def self.sort(an_array)
|
8
9
|
return an_array.sort_by{ |x| normalize(x) }
|
9
|
-
end
|
10
|
-
|
11
|
-
|
12
|
-
private
|
10
|
+
end
|
13
11
|
|
14
12
|
def self.normalize(version)
|
15
13
|
version.to_s.scan(%r/[0-9a-zA-Z]+/o).map{|i| NumberElement.new(i)}
|
16
14
|
end
|
17
15
|
|
16
|
+
|
17
|
+
private
|
18
|
+
|
18
19
|
class NumberElement
|
19
20
|
include Comparable
|
20
21
|
attr_accessor :val
|
data/lib/naturally/version.rb
CHANGED