Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.
dd 'a b'.comb.sort; (" ", " ", "a", "b", "").Seq;
If you wanted to implement your own behaviour you could just add another multi
subset CzechString of String; multi sub infix:<cmp>(CzechString $a, CzechString $b) { # sort logic here }
Or you can simply pass your comparator to sort
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] });


holli

You can lead your users to water, but alas, you cannot drown them.

In reply to Re^3: Spoiled by Perl (sorting sugar) by holli
in thread Spoiled by Perl by afoken

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2023-06-07 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (29 votes). Check out past polls.

    Notices?