Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Spoiled by Perl (sorting sugar)

by holli (Abbot)
on Oct 31, 2021 at 22:29 UTC ( #11138283=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    # cmp. Generic, "smart" three-way comparator.
    
    ...
    
    # 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.
    
  2. or download this
    dd 'a  b'.comb.sort;
    (" ", " ", "a", "b", "").Seq;
    
  3. or download this
    subset CzechString of String;
    multi sub infix:<cmp>(CzechString $a, CzechString $b) {
      # sort logic here
    }
    
  4. or download this
    sub czech-sort(String $a, String $b) {
      # sort logic here
    }
    
    @czech-words.sort( &czech-sort );
    
  5. or download this
    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] });
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11138283]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2023-03-29 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (70 votes). Check out past polls.

    Notices?