From 32129d179471b761e99e32e533b06b1104ec2f77 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Sun, 22 Dec 2024 18:29:57 +0000 Subject: [PATCH] Add smart sort to index building --- src/upload.raku | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/upload.raku b/src/upload.raku index 3a39a69..c65efd3 100755 --- a/src/upload.raku +++ b/src/upload.raku @@ -76,7 +76,22 @@ my method add-path(%index: $path, $value) { my method render-index(%index:) { # TODO sort these properly my @elements = []; - for %index.keys -> $key { + my sub compare-with-numbers($a, $b) { + my $a-match = $a ~~ /^(.+?)(\d*)(\.\w+)$/; + my $b-match = $b ~~ /^(.+?)(\d*)(\.\w+)$/; + ($a-match[0] cmp $b-match[0]) || ($a-match[1].Int <=> $b-match[1].Int) + } + my sub compare-keys($key1, $key2) { + if $key1 ~~ Str && $key2 ~~ Str { + compare-with-numbers($key1, $key2) + } elsif $key1 ~~ Str { + Order::Less + } else { + Order::More + } + } + my @keys = %index.keys.sort(&compare-keys); + for @keys -> $key { given %index{$key} { when Str { my $x = $_;