Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Hash by key values 1

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
HI,

I'm trying sort my hash by the second level keys values.
Unsorted line looks like :

for my $j ( sort keys %$jobsDB ) {

So far did not figure out how to do - even read some web threads.
My hash looks like:

$jobsDB->{'job5'}{'endTime'} = 20
$jobsDB->{'job3'}{'endTime'} = 30
$jobsDB->{'job2'}{'endTime'} = 40

I need now to use the first keys of hash (jobx) sorted by values of the second level.
Will appreciate help.
thanks

Long live king Moshiach !
 
This might help:
Code:
my $jobsDB;
$jobsDB->{'job5'}{'endTime'} = 20;
$jobsDB->{'job3'}{'endTime'} = 30;
$jobsDB->{'job2'}{'endTime'} = 40;

my @keys =  sort {$jobsDB->{$a}{'endTime'} <=>
			$jobsDB->{$b}{'endTime'}} keys %$jobsDB;
print "$_\n" for @keys;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top