Skip to content
Snippets Groups Projects
Commit 8339ff1d authored by Henk's avatar Henk
Browse files

Make sort_by_alphanum consistent for chunks that parse to the same int like 04 and 4.

parent 35e0284e
No related branches found
No related tags found
1 merge request!47Make sort_by_alphanum stable
......@@ -26,7 +26,7 @@ module Enumerable
a_chunk, a = extract_alpha_or_number_group(a)
b_chunk, b = extract_alpha_or_number_group(b)
ret = if a_chunk =~ /\d/ and b_chunk =~ /\d/
ret = if a_chunk =~ /\d/ && b_chunk =~ /\d/ && a_chunk.to_i != b_chunk.to_i
a_chunk.to_i <=> b_chunk.to_i
else
a_chunk <=> b_chunk
......
......@@ -14,5 +14,10 @@ describe Enumerable do
it 'compares number chunks as integers' do
expect(%w(004 3).sort_by_alphanum).to eq %w(3 004)
end
it 'sorts identical integers by asci' do
input = %w[b4e b0004e b04e b004e]
expect(input.sort_by_alphanum).to eq %w[b0004e b004e b04e b4e]
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment