Skip to content

Instantly share code, notes, and snippets.

@thundergnat
Last active September 20, 2017 23:26
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/3d71a3e1b24feec533612dd7e2fa94e6 to your computer and use it in GitHub Desktop.
Save thundergnat/3d71a3e1b24feec533612dd7e2fa94e6 to your computer and use it in GitHub Desktop.
Natural collation
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