- 论坛徽章:
- 145
|
本帖最后由 jason680 于 2014-08-04 20:50 编辑
回复 7# zhangfenyun
Here you are
Note: There are many issues on your procedure rule
$ cat sort_50123x.pl
#!/bin/env perl
use strict;
use warnings;
# There is simple way to create a hash of increase value by an array
# my @aSort = qw/2055 0 1 2 3 590 980 3175/;
# my %hSort = map{ $aSort[$_] => $_ } 0 .. @aSort-1;
my %hSort = (
'2055' => 1,
'0' => 2,
'1' => 3,
'2' => 4,
'3' => 5,
'590' => 6,
'980' => 7,
'3175' => 8
);
my @a = (600,900,2055,1551,1401,3175,590,980,3203,3503);
my @a_sorted = sort by50123 @a;
print "@a_sorted\n";
sub by50123 {
#my($a, $b) = @_;
my($sA, $sB) = (9, 9);
$sA = $hSort{$a%10} if(exists $hSort{$a%10});
$sB = $hSort{$b%10} if(exists $hSort{$b%10});
$sA = $hSort{$a} if(exists $hSort{$a});
$sB = $hSort{$b} if(exists $hSort{$b});
#print "A=$sA($a), B=$sB($b)\n";
$sA <=> $sB ||
$a <=> $b;
}
$ perl sort_50123x.pl
2055 600 900 1401 1551 3203 3503 590 980 3175
|
|