how do you decide its a cmp or <=> ?
The dispatcher does that by finding the appropriate multi-sub that implements the actual comparison.
# cmp. Generic, "smart" three-way comparator. multi sub infix:<cmp>(Any, Any) multi sub infix:<cmp>(Real:D, Real:D) multi sub infix:<cmp>(Str:D, Str:D) multi sub infix:<cmp>(Version:D, Version:D) # Compares strings with string semantics, numbers with number semantic +s, Pair objects first by key and then by value etc. # if $a eqv $b, then $a cmp $b always returns Order::Same. Otherwise O +rder::Less or Order::More.
what if you want to apply another ordering? Like Czech sorting?Rakus Str class is fully Unicode aware and by default sorts characters on the numeric value of the codepoint.
If you wanted to implement your own behaviour you could just add another multidd 'a ä b'.comb.sort; (" ", " ", "a", "b", "ä").Seq;
Or you can simply pass your comparator to sortsubset CzechString of String; multi sub infix:<cmp>(CzechString $a, CzechString $b) { # sort logic here }
sub czech-sort(String $a, String $b) { # sort logic here } @czech-words.sort( &czech-sort );
how do you sort by multiple criteria? Method chaining?You can adress that in the comparator like in Perl, or make the comparator return a list of things.
dd ([1,'z'], [2,'a'], ['1', 'a']).sort( *.[0,1] ); (["1", "a"], [1, "z"], [2, "a"]).Seq dd ([1,'z'], [2,'a'], ['1', 'a']).sort( { $^a[0] <=> $^b[0] || $^a[1] +cmp $^b[1] });
In reply to Re^3: Spoiled by Perl (sorting sugar)
by holli
in thread Spoiled by Perl
by afoken
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |