Skip to content

Instantly share code, notes, and snippets.

@likhatskiy
Created November 18, 2009 07:50
Show Gist options
  • Save likhatskiy/237633 to your computer and use it in GitHub Desktop.
Save likhatskiy/237633 to your computer and use it in GitHub Desktop.
double list sort
#Двойная Сортировка массива хешей
#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