Last active
January 20, 2022 09:51
-
-
Save Hampei/3f5e48f43f9068c3a11218a5a8f007c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on Dave Koelle's Alphanum algorithm | |
# Henk van der Veen 2022 | |
# Inspired by https://github.com/aiekick/alphanum | |
# Released under the MIT License - https://opensource.org/licenses/MIT | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the "Software"), | |
# to deal in the Software without restriction, including without limitation | |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
# and/or sell copies of the Software, and to permit persons to whom the | |
# Software is furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included | |
# in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
# USE OR OTHER DEALINGS IN THE SOFTWARE. | |
module Enumerable | |
# divides strings into chunks of all numbers, all letters or all other characters. | |
# per chunk: ALPHA > NUM > NON_ALPHANUM | |
# When same type: Compare different numbers by integer-comparison, anything else by normal string-comparison. | |
def sort_by_alphanum | |
sort do |a, b| | |
if block_given? | |
grouped_compare(yield(a), yield(b)) | |
else | |
grouped_compare(a, b) | |
end | |
end | |
end | |
def sort_by_alphanum! | |
sort! do |a, b| | |
if block_given? | |
grouped_compare(yield(a), yield(b)) | |
else | |
grouped_compare(a, b) | |
end | |
end | |
end | |
private | |
# License: MIT | |
# Copyright: Henk van der Veen | |
ALL_NUM = /\d+/ | |
ALL_ALPHA = /[A-Za-z]+/ | |
NON_ALPHANUM = /[^A-Za-z0-9]+/ | |
def grouped_compare(a, b) | |
a_scanner = StringScanner.new(a) | |
b_scanner = StringScanner.new(b) | |
# each loop has to do exactly 1 non-nil-scan on both scanners or return a non-zero value. | |
loop do | |
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 | |
a_chunk.to_i <=> b_chunk.to_i | |
else # 03 vs 3 | |
a_chunk <=> b_chunk | |
end | |
elsif b_scanner.scan(ALL_ALPHA) | |
-1 | |
else # b is NON_ALPHANUM | |
1 | |
end | |
elsif (a_chunk = a_scanner.scan(ALL_ALPHA)) | |
if (b_chunk = b_scanner.scan(ALL_ALPHA)) | |
a_chunk <=> b_chunk | |
else # b is ALL_NUM or NON_ALPHANUM | |
1 | |
end | |
else # a is NON_ALPHANUM | |
a_chunk = a_scanner.scan(NON_ALPHANUM) | |
if (b_chunk = b_scanner.scan(NON_ALPHANUM)) | |
a_chunk <=> b_chunk | |
else # b is ALL_NUM or ALL_ALPHA | |
-1 | |
end | |
end | |
return ret if ret != 0 | |
end | |
end | |
end | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment