IT Community - Software Programming, Web Development and Technical Support

How do I sort a hash by the hash value?

This is a discussion on How do I sort a hash by the hash value? within the Perl forums, part of the Software Development category; Hi buddies... can any one tell me...How do I sort a hash by the hash value?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Perl

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 11-16-2007, 09:42 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 905
S.Vinothkumar is on a distinguished road
Question How do I sort a hash by the hash value?

Hi buddies...

can any one tell me...How do I sort a hash by the hash value?
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 11-26-2007, 05:42 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Smile Re: How do I sort a hash by the hash value?

Here's a program that prints the contents of the grades hash, sorted numerically by the hash value. Check it out…

Code:
#!/usr/bin/perl -w 

# Help sort a hash by the hash 'value', not the 'key'. 
to highest). 
sub hashValueAscendingNum { 
$grades{$a} <=> $grades{$b}; 
} 



# Help sort a hash by the hash 'value', not the 'key'. 
# Values are returned in descending numeric order 
# (highest to lowest). 
sub hashValueDescendingNum { 
$grades{$b} <=> $grades{$a}; 
} 


%grades = ( 
student1 => 90, 
student2 => 75, 
student3 => 96, 
student4 => 55, 
student5 => 76, 
); 

print "\n\tGRADES IN ASCENDING NUMERIC ORDER:\n"; 
foreach $key (sort hashValueAscendingNum (keys(%grades))) { 
print "\t\t$grades{$key} \t\t $key\n"; 
} 

print "\n\tGRADES IN DESCENDING NUMERIC ORDER:\n"; 
foreach $key (sort hashValueDescendingNum (keys(%grades))) { 
print "\t\t$grades{$key} \t\t $key\n"; 
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 11-26-2007, 05:56 AM
velhari velhari is offline
D-Web Programmer
 
Join Date: Mar 2007
Location: Chennai
Posts: 59
velhari is on a distinguished road
Send a message via AIM to velhari
Default Re: How do I sort a hash by the hash value?

Refer this URL:- Learn Perl: Perl Q&A: How do I sort a hash by the hash "value"?
__________________
Regards,
VELHARI
I am not totally useless. I can be used for a bad example
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort Datatable oxygen C# Programming 2 04-22-2008 09:25 PM
How can we sort the values in dataset? mobilegeek C# Programming 4 04-18-2008 03:39 AM
Overview of Hash table. H2o Other Web Programming Languages 1 09-14-2007 06:28 AM
What’s the difference between Hash Map and Hash Table? mobilegeek Java Programming 2 09-14-2007 05:37 AM
What is a hash? vadivelanvaidyanathan Perl 1 07-30-2007 06:29 AM


All times are GMT -7. The time now is 06:23 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0