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

topeg
 2011-04-14 18:50
#147765 #147765
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
lt returns true or false. If you want to do a sort you need three values if one value is smaller, equal or bigger. For strings use cmp for numbers <=>.

Example:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@t = ( {b=>1,w=>10},
       {b=>3,w=>30},
       {b=>2,w=>40},
       {b=>7,w=>60},
       {b=>4,w=>70},
       {b=>6,w=>90},
       {b=>5,w=>100});

print "## RAW #####################\n";
pout(@t);

@bb = sort func @t;

sub func{
  return $a->{b} <=> $b->{b};
}

print "## SORTED ##################\n";
pout(@bb);


sub pout
{
  foreach $f1 (@_){
    foreach $f2 (sort keys %$f1){
      print "$f2 $f1->{$f2}\n";
    }
    print "------\n";
  }
}


ps. Your name looks German. This is a forum for German natives, so if possible please use German.

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