From 8339ff1d9e7f815b0ce2279c0869f056e310d0a4 Mon Sep 17 00:00:00 2001 From: Henk <henk.van.der.veen@gmail.com> Date: Mon, 17 Jan 2022 10:16:40 +0100 Subject: [PATCH] Make sort_by_alphanum consistent for chunks that parse to the same int like 04 and 4. --- lib/roqua/core_ext/enumerable/sort_by_alphanum.rb | 2 +- spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/roqua/core_ext/enumerable/sort_by_alphanum.rb b/lib/roqua/core_ext/enumerable/sort_by_alphanum.rb index 0ca75c6..2f1ee82 100644 --- a/lib/roqua/core_ext/enumerable/sort_by_alphanum.rb +++ b/lib/roqua/core_ext/enumerable/sort_by_alphanum.rb @@ -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 diff --git a/spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb b/spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb index 9de0b2f..db112e7 100644 --- a/spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb +++ b/spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb @@ -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 -- GitLab