Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Sort: By keys, values, and their returning values.

by larsss31 (Acolyte)
on Nov 25, 2009 at 11:03 UTC ( #809315=perlquestion: print w/replies, xml ) Need Help??

larsss31 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! I have a question regarding the sort function. Can you confirm to me that, having an hash '%hash', and wanting to sort its keys by key, hence returning a list of sorted keys (assuming a monodimensional hash, with numeric sorting), i should use:
sort { $a <=> $b } keys %hash
and if i wanted to sort it by its values, again obtaining a list of sorted keys:
sort { $hash{$a} <=> $hash{$b} } keys %hash
so far, so good.
Now, i want to sort my hash, but by its values, i use "values", and, to sort by value, i should use:
sort { $a <=> $b } values %hash
and if i want to sort by keys i use
sort { $hash{$a} <=> $hash{$b} } values %hash
Am I right? If not, please tell me what's wrong.
If I am, there must be some problem in the last sort, because it does not return me a list of values sorted by keys, it just does simply nothing (at least, in my real code, not this fictional example).

thanks in advance,
larss

Replies are listed 'Best First'.
Re: Sort: By keys, values, and their returning values.
by BioLion (Curate) on Nov 25, 2009 at 11:18 UTC

    Close, but no cigar:

    sort { $hash{$a} <=> $hash{$b} } values %hash;

    This is taking an array of hash values (returned by values %hash, and treating them as keys. And unless your values are also available as keys ( i.e. my %hash = ( key1 => 'key2', key2 => 'key1',); ) it will not give you the answer you are hoping for!

    The best approach is to keep it simple:

    # list of sorted keys in numeric order my @keys = sort { $a <=> $b } keys %hash; # list of values in numeric order my @values = sort { $a <=> $b } values %hash; # a list of the *keys* sorted by their corresponding value my @keys_by_value = sort { $hash{$a} <=> $hash{$b} } keys %hash;

    Hope this makes things clearer!? Also remember their is plenty of other references out there on perldoc:

      keys
      values
      sort
    Good hunting!

    Just a something something...
Re: Sort: By keys, values, and their returning values.
by jethro (Monsignor) on Nov 25, 2009 at 11:31 UTC
    To get the values sorted by key (your case 4), you probably need two steps:
    map { $hash{$_}} sort { $a <=> $b } keys %hash;
Re: Sort: By keys, values, and their returning values.
by salva (Canon) on Nov 25, 2009 at 11:29 UTC
    sorting values by key:
    my @keys_by_key = sort { $a <=> $b } keys %hash; my @values_by_key = @hash{@keys_by_key};
    or in one single sentence:
    my @values_by_key = @hash{sort { $a <=> $b } keys %hash};
Re: Sort: By keys, values, and their returning values.
by kyle (Abbot) on Nov 25, 2009 at 15:16 UTC

    If you wanted to sort keys by their values, and then by key (when the values are identical), you could do it this way:

    use 5.010; my %h = ( a => 1, b => 1, c => 2, ); say join '|', sort { $hash{$a} <=> $hash{$b} || $a cmp $b } keys %h; __END__ a|b|c

    In this example, I've sorted the keys using cmp since they're strings, but you can use <=> there too, if that will work better for what you're doing.

Perl 6 sorting (was: Re: Sort: By keys, values, and their returning values.)
by moritz (Cardinal) on Nov 26, 2009 at 10:50 UTC
    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.
      Having Pair objects and a built-in Schwartzian transform makes sorting really simple and powerful

      I hope you are not really using the Schwartzian transform in Rakudo as it is one of the worst ways to optimize a sort-by-generated-key operation!

      Something as...

      @keys = map gen_key($_), @data; @sorted = @data[sort {$keys[$a] <=> $keys[$b]} 0..$#keys];
      should perform much better when correctly implemented in a low level language, specially if you are able to use packed arrays to store @keys.
        I hope you are not really using the Schwartzian transform in Rakudo as it is one of the worst ways to optimize a sort-by-generated-key operation!

        Actually it's an implementation detail about which I don't care really much. Correctness first, performance later. I just called it a Schwartzian Transform because that's the name that most perl programmers use for the sort-by-generated-key operation.

        Looking at the source it seems to do something close to what you proposed.

        Perl 6 - links to (nearly) everything that is Perl 6.
Re: Sort: By keys, values, and their returning values.
by Anonymous Monk on Nov 25, 2009 at 22:15 UTC
    Hashes are one way by their nature, so you can only do a sort (that results in a "sorted hash") based on the upstream (key) end. You can sort the hash by key or value, as you have done in (#1) and (#2) --- (in reality you're returning a list of keys corresponding to the hash ordered by key(#1) or value(#2).)

    In (#3) you are not sorting the hash, you are sorting the values in the hash and discarding the keys. This gets you a sorted value list (which may be all you want, depending on the application)

    In (#4) you are passing the sort function the values in the hash and trying to use them like keys. (again, discarding the actual keys). Hopefully it is obvious now that this will not do anything useful.

    -Greg

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://809315]
Approved by BioLion
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2021-09-27 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?