Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Allow me show off some Perl 6 features here.

If you use a hash in list context in Perl 6, you get a list of pairs, each holding a key and a value.

Since hashes are object like everything else, you can call methods on them:

my @sorted_keys = %hash.keys.sort

Since values are typed (ie you can ask a variable if it contains a number, a string or something entirely different), the default comparison is smart enough to compare strings with string semantics and numbers with number semantics. So if the values are numbers already, there's no need to explicitly use <=>

It is even smart enough to compare pairs, so when you call %hash.sort directly, it sorts primary by key and secondary by value.

What if you want to sort the keys by value? You use the built-in Schwartzian transform:

%hash.keys.sort({ %hash{$_} });

Since the block { %hash{$_} } knows its number of parameters, the sort method can find out that the block takes not two but one parameter, and sorts by the return value of the block, while still returning the original value.

If you want to do that on an anonymous hash, you can sort the pairs instead:

# return a random, anonymous hash: sub h { return %( (1..10).pick(*) ) } # and get the keys sorted numerically by value: my @keys = h().sort( { .value })>>.key

This sorts the pairs by value, and then calls the .key method on each list item. (so >>.key is short for .map({ .key }), except that the compiler is allowed to execute the >>.key calls out of order, and parallelize them)

Having Pair objects and a built-in Schwartzian transform makes sorting really simple and powerful. And Rakudo implements all of this already. Have fun experimenting!

Perl 6 - links to (nearly) everything that is Perl 6.

In reply to Perl 6 sorting (was: Re: Sort: By keys, values, and their returning values.) by moritz
in thread Sort: By keys, values, and their returning values. by larsss31

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the shadows deepen...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2018-02-22 23:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      When it is dark outside I am happiest to see ...














      Results (300 votes). Check out past polls.

      Notices?