juo has asked for the wisdom of the Perl Monks concerning the following question:
I have found the Perlfaq 4 very usefull. I found their a sample to do sorting of hash table by value : Alphabetic and Numeric but I could not find on how to do it Alpha-Numeric. Alpha Numeric : 1 2 12 a9 a12 ---- Numeric : 1 2 12 (cannot have characters) ----- Alphabetic : 1 2 13 a12 a9 ------- Anybody has an idea on how to do this. See example code below for numeric only :
For example : $keys{a} = 2; $keys{b} = 1; $keys{c} = 12; $keys{d} = 'a12'; $keys{d} = 'a9'; # 3 = sort by value - numeric ascending- will not work if contain char +acters foreach my $key (sort {$keys{$a} <eq> $keys{$b} || length($a) <=> le +ngth($b) || $a cmp $b} (keys %keys)) { print "Key = $key, Value = $keys{$key}\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash sort by value - Alpha Numeric
by sauoq (Abbot) on May 27, 2003 at 08:36 UTC | |
|
Re: hash sort by value - Alpha Numeric
by bart (Canon) on May 27, 2003 at 08:59 UTC | |
|
Re: hash sort by value - Alpha Numeric
by Enlil (Parson) on May 27, 2003 at 09:46 UTC |