codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  sort hash by value, numerically  hermanningjaldsson at 13:48 on Tuesday, March 04, 2008
 

#the following code almost sorts the has by value.
#how can i completely sort the has by value?


%ages = (hmm=>1, siggs=>2, john=>23, anthony=>99, olav=>22, lila=>17);


foreach $key (sort {$ages {$a} cmp $ages {$b}} keys %ages )
{
print STDOUT "$ages{$key}\n";
}


  Re: sort hash by value, numerically  amitbhosale at 07:00 on Monday, March 10, 2008
 

Hi,

you can use the number comparison operator <=>.
With the form op1 <=> op2 this operator returns 1 if op1 is greater than op2, 0 if op1 equals op2, and -1 if op1 is less than op2.


#!/usr/bin/perl -w


%ages = (hmm=>1, siggs=>2, john=>23, anthony=>99, olav=>22, lila=>17);


foreach $key (sort { $ages {$a} <=> $ages {$b}} keys %ages )
{
print STDOUT "$ages{$key}\n";
}

========o/p==========
$ perl test.pl
1
2
17
22
23
99


  Re: sort hash by value, numerically  hermanningjaldsson at 08:20 on Monday, March 10, 2008
 

Thanks a lot.



  Re: sort hash by value, numerically  arash farhad at 04:54 on Wednesday, March 10, 2010
 

Nice post about numerically. I like your blog very much because it has very interesting articles of different topics like 70-293 braindumps, 70-270 dumps and ccna exam dumps and their tips and tricks. I am a very big googler and search on different topics. Between searching i found your nice blog. Thanks for your this great blog.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Need some help with javascript code in Unity!
•  Re: Copy files from one directory to another in a local network
•  Re: number of hours and minutes spent
•  Re: Saving a Picture or Image contained within a Picturebox into Windows Registry
•  Join Apache Lucene EuroCon 2010
•  FREE dating software worth over $350 for free
•  FREE dating software worth over $350 for free
•  running a command in remote machine by using perl
•  converting JAVA code to JSP


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2010