Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hashes are always unordered (unless you use special modules), and so the two hashes you have posted above are identical. The most common desire is to output hashes in a sorted manner, or, in your case, you seem to want to store the keys in a sorted order in an array. However, note that the way you have posted the data above is not runnable code, and it makes it unclear what the actual hash format is - see How do I post a question effectively? and Short, Self-Contained, Correct Example. I have made a guess about the hash's format below.

As far as I can tell, you only need to loop over the keys of the outer hash, and then sort the keys of the inner hash based on their value. That would look like this:

use warnings; use strict; use Data::Dumper; my %rankBased = ( 190 => { test1 => { score => '13.28' }, test2 => { score => '-47.56' }, test3 => { score => '18.50' }, test4 => { score => '14.88' }, }, 191 => { test1 => { score => '9.18' }, test2 => { score => '2.84' }, test3 => { score => '15.62' }, test4 => { score => '11.84' }, }, ); for my $key1 (keys %rankBased) { my @sort_by_rank = sort { $rankBased{$key1}{$b}{score} <=> $rankBased{$key1}{$a}{score} } keys %{$rankBased{$key1}}; print Dumper(\@sort_by_rank); } __END__ $VAR1 = [ 'test3', 'test4', 'test1', 'test2' ]; $VAR1 = [ 'test3', 'test4', 'test1', 'test2' ];

Update: See also "How do I sort a hash (optionally by value instead of key)?" in perlfaq4 and "Access and Printing of a Hash of Hashes" in perldsc.


In reply to Re: Sorting perl hash by haukex
in thread Sorting perl hash by paul05

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?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-25 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found