문제

I have the list as

@emprecords = (
                  ['pavan',24,25000],
                  ['kumar',25,35000],
                  ['ajay',22,35000],
                  ['vijay',25,20000]
);

i need to sort them by lowest age first with highest slary first .

도움이 되었습니까?

해결책

Use <=> for a numeric comparison and a conditional or to check the salary when age is equal:

#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

my @emprecords = sort {
                $a->[1] <=> $b->[1]
                        or
                $b->[2] <=> $a->[2]
                }
                (  ['pavan',24,25000],
                  ['kumar',25,35000],
                  ['ajay',22,35000],
                  ['vijay',25,20000]
);

print Dumper \@emprecords;

Run it like:

perl script.pl

That yields:

$VAR1 = [
          [
            'ajay',
            22,
            35000
          ],
          [
            'pavan',
            24,
            25000
          ],
          [
            'kumar',
            25,
            35000
          ],
          [
            'vijay',
            25,
            20000
          ]
        ];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top