由值排序hash
foreach $value (sort {$coins{$a} cmp $coins{$b} } keys %coins)
{
print “$value $coins{$value}”;
}
# ref :http://note.tcc.edu.tw/285.html
sort 以 ASCII 順序來將陣列排序
@a =qw(11 1 5 7 2);
@b = sort @a;
print join ‘,’,@b,” “;
sort 以數值大小排序
@a =qw(11 1 5 7 2);
@b = sort {$a<=>$b} @a;
print join ‘,’,@b,” “;
sort 排序 IP
use strict;
my @ip = (“140.21.135.218”, “140.112.22.49”, “140.213.21.4”, “140.211.42.8”);
my @order = sort ipsort @ip; # 直接叫用副常式
print “$_\n” for @order;
sub ipsort {
my ($a1, $a2, $a3, $a4) = $a =~ /(\d+).(\d+).(\d+).(\d+)/; # 分為四個數字
my ($b1, $b2, $b3, $b4) = $b =~ /(\d+).(\d+).(\d+).(\d+)/;
$a1 <=> $b1 or $a2 <=> $b2 or $a3 <=> $b3 or $a4 <=> $b4; # 進行多子鍵排序
}
留言