Last active
September 20, 2017 23:26
-
-
Save thundergnat/3d71a3e1b24feec533612dd7e2fa94e6 to your computer and use it in GitHub Desktop.
Natural collation
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
sub natural ($a) { $a.subst(/(\d+)/, ->$/ {0~$0.chars~$0},:g)~$a } | |
sub ncollate (@list){ # this works... but yuck | |
@list[@list.kv.map( { [$^k, $^v.&natural] } ).sort( { $^a[1] unicmp $^b[1] } )[*;0]] | |
}; | |
my @words = <32nd 3rd 144th 17th 2 95 apple ball Bald | |
Æon æon aether Évian evoke außen Autumn file3 file21 file17>; | |
#say @words.sort; | |
#say @words.collate; | |
#say @words.sort( &[coll] ); | |
#say @words.sort( *.&natural ); | |
#say @words.sort( *.fc.&natural ); | |
say @words.&ncollate; | |
# if the collate transfor routine is exposed, we can then use it a single | |
# arity tranform routine to sort... which can then easily be chained. | |
# Instead of the ugly ncollate routine I am envisioning something like: | |
#say @words.sort( *.&natural.&collated ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment