Skip to content

Commit

Permalink
Fixes issue where blank string would break sorting (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertg authored Oct 19, 2024
1 parent 121655f commit 3a6cbc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/natural_sort/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def check_regexp(sa, sb)
ma, mb = multireg(REGEXP,sa), multireg(REGEXP,sb)
it = 0
equal = 0
ret = ['', '']
ret = [sa, sb]
while (it < [ma.size,mb.size].min) and (equal==0)
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])
l = [ma[it][2].size,mb[it][2].size].max
Expand Down
17 changes: 11 additions & 6 deletions test/test_natural_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

class MyClass
include NaturalSort

def initialize(array = TestHelper::SimpleUnsorted)
@array = array
end

def to_a
@array
end
Expand All @@ -23,21 +23,21 @@ def test_self
# Test using include
class TestNaturalSort < Minitest::Test
include NaturalSort

def setup
@obj = MyClass.new
end

def test_case_sensitive
sorted = @obj.natural_sort
assert_equal TestHelper::SimpleSorted, sorted
end

def test_mixed
obj = MyClass.new %w(a1 a12 A11 a2 a10 A3 a21 A29)
assert_equal %w(a1 a2 A3 a10 A11 a12 a21 A29), obj.natural_sort
end

def test_numbers
obj = MyClass.new %w(a1 a12 a11 a2 a10 a3 a21 a29)
assert_equal %w(a1 a2 a3 a10 a11 a12 a21 a29), obj.natural_sort
Expand All @@ -52,4 +52,9 @@ def test_number_leading_zero
obj = MyClass.new %w(A001 A08 A007 A003 A011 A20 A200)
assert_equal %w(A001 A003 A007 A08 A011 A20 A200), obj.natural_sort
end

def test_empty_strings
obj = MyClass.new ['A', 'B', 'C', '', 'A', 'B', 'C']
assert_equal ['', 'A', 'A', 'B', 'B', 'C', 'C'], obj.natural_sort
end
end

0 comments on commit 3a6cbc2

Please sign in to comment.