Created
November 18, 2009 07:50
-
-
Save likhatskiy/237633 to your computer and use it in GitHub Desktop.
double list sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Двойная Сортировка массива хешей | |
#mysort(array_ref, up_sort, down_sort) | |
#up_sort - индекс сортировки по возрастанию | |
#down_sort - индекс сортировки по убыванию | |
sub mysort { | |
my ($arr, $up, $down) = @_; | |
my $temp; | |
push @{$temp->{$_->{$up}}}, $_ for @$arr; | |
return [map { | |
sort {$b->{$down} <=> $a->{$down}} @{$temp->{$_}} | |
} sort {$a <=> $b} keys %$temp]; | |
} | |
=pod | |
use Data::Dumper; | |
$\ = "\n"; | |
my $t = [ | |
{ | |
'a' => 0, | |
'b' => 1, | |
}, | |
{ | |
'a' => 0, | |
'b' => 4, | |
}, | |
{ | |
'a' => 2, | |
'b' => 0, | |
}, | |
]; | |
print Dumper mysort($t, "a", "b"); | |
output: | |
$VAR1 = [ | |
{ | |
'a' => 0, | |
'b' => 4 | |
}, | |
{ | |
'a' => 0, | |
'b' => 1 | |
}, | |
{ | |
'a' => 2, | |
'b' => 0 | |
} | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment