Thread How to sort Array with hashref Elements and custom search function (5 answers)
Opened by Frank Schuster at 2011-04-14 17:58

rosti
 2011-04-14 18:37
#147764 #147764
User since
2011-03-19
3202 Artikel
BenutzerIn
[Homepage]
user image
Quote
Can anybody enlighten me?

Later?

Please take a little look at my Solution

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use strict;
use warnings;
use Data::Dump qw(dump);


#my @t = ( {'b','1','w','10'},{'b','3','w','30'},{'b','2','w','40'});
# at first, making hash readable as such
my @t = ( {'b' => '1','w' => '10'},{'b' => '3','w' =>'30'},{'b' => '2','w' => '40'});
# consider issue: the value in key 'b' is numeric, use spaceship '<=>' for compare

# transform by Randal Schwartz
@t = 
        map{$_->[0]}
        sort{$a->[1] <=> $b->[1]}
        map{ [$_, $_->{b}] }@t;

print dump \@t;

View full thread How to sort Array with hashref Elements and custom search function