Skip to content

Instantly share code, notes, and snippets.

@thundergnat
Last active September 23, 2017 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thundergnat/8a48575bbedfed8c4ed5297777e85874 to your computer and use it in GitHub Desktop.
Save thundergnat/8a48575bbedfed8c4ed5297777e85874 to your computer and use it in GitHub Desktop.
Collation numeric sorting test
# Testing against https://github.com/samcv/MoarVM/tree/natural
# using a Perl6 / NQP build against that branch.
# For these very limited character sets, I would expect these to return
# essentially the same results, except perhaps for digit clusters with leading zeros
use experimental :collation;
# Ideally, numerics would sort before alphabetics; the UCA sorts
# numbers after letters so that may not be practical.
# Very limited naural sorting transform to sort numbers after alphabetics
# Traditionally numbers are natural sorted before alphabetics. This sub has been
# modified to mimic the UCA 'sorting' order of numerics after alphabetics.
# To get the traditional behavior, change 'ÿ' to '0' in the below sub.
# Note that this natural transform doesn't really handle numbers with
# leading zeros very well.
sub natural ($a) { $a.subst(/(\d+)/, ->$/ {'ÿ'~$0.chars~$0},:g)~$a }
# small assortment of test codepoints
my @chars = <0 1 2 3 4 5 6 7 8 9 a b c d e>;
#random strings
for ^50 {
my @strings = map { @chars.roll((1..10).pick).join }, ^10;
my $n = @strings.sort: *.&natural;
my $c = @strings.collate;
# only print the strings if they are different
if $n ne $c {
say $n;
say $c;
say '-' x 60;
}
}
# IP addresses - a very common natural sort application
my @ips = ((0..255).roll(4).join('.') for ^20);
my @n = @ips.sort: *.&natural;
my @c = @ips.sort: &[coll];
printf "%-15s : %s\n", 'natural sort', 'collate';
for @n.kv -> $k, $v {
printf "%-15s : %s\n", $v, @c[$k];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment