From 894d7109caed5fddc580bf49cfc84b83e914f8b8 Mon Sep 17 00:00:00 2001
From: Henk van der Veen <henk.van.der.veen@roqua.nl>
Date: Thu, 20 Jan 2022 09:52:47 +0100
Subject: [PATCH] Hh sort by alphanum shorter first

---
 CHANGELOG.md                                            | 4 ++++
 lib/roqua-support/version.rb                            | 2 +-
 lib/roqua/core_ext/enumerable/sort_by_alphanum.rb       | 2 ++
 spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb | 5 +++++
 4 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e390e97..2c4ab66 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+##  0.4.3
+
+* Make sort_by_alphanum sort shorter string first.
+
 ## 0.4.2
 
 * Make sort_by_alphanum stable for chunks that parse to the same int like 04 and 4.
diff --git a/lib/roqua-support/version.rb b/lib/roqua-support/version.rb
index fc7d84c..31b036c 100644
--- a/lib/roqua-support/version.rb
+++ b/lib/roqua-support/version.rb
@@ -1,5 +1,5 @@
 module Roqua
   module Support
-    VERSION = '0.4.2'.freeze
+    VERSION = '0.4.3'.freeze
   end
 end
diff --git a/lib/roqua/core_ext/enumerable/sort_by_alphanum.rb b/lib/roqua/core_ext/enumerable/sort_by_alphanum.rb
index e25a85f..0c47641 100644
--- a/lib/roqua/core_ext/enumerable/sort_by_alphanum.rb
+++ b/lib/roqua/core_ext/enumerable/sort_by_alphanum.rb
@@ -33,6 +33,8 @@ module Enumerable
       ret = \
         if a_scanner.eos?
           -1
+        elsif b_scanner.eos?
+          1
         elsif (a_chunk = a_scanner.scan(ALL_NUM))
           if (b_chunk = b_scanner.scan(ALL_NUM))
             if a_chunk.to_i != b_chunk.to_i
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 8de7e5c..c8b8c13 100644
--- a/spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb
+++ b/spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb
@@ -11,6 +11,11 @@ describe Enumerable do
       expect(input.sort_by_alphanum(&:reverse)).to eq ["004some10thing", "004some11thing", "3another"]
     end
 
+    it 'sorts shorter strings first' do
+      input = %w[a_2_ a_2 a a_1]  # curlies are above alpha in utf-8, Ԙ is multi-byte
+      expect(input.sort_by_alphanum).to eq %w[a a_1 a_2 a_2_]
+    end
+
     it 'treats non-alphanum as lower than alpha and num' do
       input = %w[b3a b{c bԘb]  # curlies are above alpha in utf-8, Ԙ is multi-byte
       expect(input.sort_by_alphanum).to eq %w[b{c bԘb b3a]
-- 
GitLab