- 论坛徽章:
- 145
|
回复 13# sjdy521
How about this, assign sort direction by yourself
direction 0: small to big (or A to Z and a to z)
direction 1: big to small (or z to a and Z to A)
_by_word_number(1,1,0,0)
$ cat sort_word_number.pl
use strict;
use warnings;
my %hash = (
'1A2a02'=>0.122,
'1A02b1'=>0.121,
'1A02a1'=>0.121,
'1B20a3'=>0.1203,
'1B21a3'=>0.1213,
'1A2a1'=>0.121,
'1Aa2a1'=>0.121,
'1A3'=>0.5,
'1A10'=>0.4,
'1A8'=>0.7,
'2B1a'=>0.41,
'2B1'=>0.4,
'2B3'=>0.2,
'2B10'=>0.4,
'2B7'=>0.1,
);
sub _by_word_number{
my @aDir = @_;
my @aB = split(/(\d+)/,$b);
shift @aB if($aB[0] eq "");
my $sRet;
#print "$b,$a,@aDir\n";
foreach(split(/(\d+)/,$a)){
next if(m/^$/);
my $sB = shift @aB;
#print "$_,$sB,@aDir\n";
if(m/\d+/){
$sRet = ($_ <=> $sB);
}
else{
$sRet = ($_ cmp $sB);
}
# to get the directory by @aDir
my $sDir = shift @aDir;
next if($sRet == 0);
$sRet *= -1 if($sDir);
return $sRet;
}
}
foreach( sort{ _by_word_number(1,1,0,0)} keys %hash){
print "$_=$hash{$_}\n";
}
|
|