Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: sort HoH by second level values in specific order

by 1nickt (Canon)
on Dec 15, 2017 at 15:45 UTC ( [id://1205596]=note: print w/replies, xml ) Need Help??


in reply to sort HoH by second level values in specific order

See sort, which demonstrates how to create a custom sort routine:

use strict; use warnings; use feature 'say'; my $free_space = { node1 => { cores => 12, mem => 200 }, node2 => { cores => 12, mem => 500 }, node3 => { cores => 10, mem => 600 }, node4 => { cores => 11, mem => 500 }, node5 => { cores => 16, mem => 800 }, node6 => { cores => 16, mem => 800 }, }; for ( sort my_sort keys %{ $free_space } ) { say "$free_space->{$_}->{'cores'} $free_space->{$_}->{'mem'} $_"; } sub my_sort { $free_space->{$b}->{'cores'} <=> $free_space->{$a}->{'cores'} || $free_space->{$b}->{'mem'} <=> $free_space->{$a}->{'mem'} || $a cmp $b }
Output:
$ perl 1205589.pl 16 800 node5 16 800 node6 12 500 node2 12 200 node1 11 500 node4 10 600 node3

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: sort HoH by second level values in specific order
by lener (Initiate) on Dec 15, 2017 at 17:51 UTC
    Thanks, that's an excellent answer. I wasn't aware that you can define your own complex sort routines.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found