Thread Sort Array of Hashes by Key (7 answers)
Opened by devrand at 2012-09-18 21:12

Linuxer
 2012-09-18 22:05
#161912 #161912
User since
2006-01-27
3882 Artikel
HausmeisterIn

user image
Beispielsweise so:

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
#! /usr/bin/env perl
use strict;
use warnings;


my @unsorted = (
    { 'ZLANED' => [ { 'CO' => 'AAA' }, { 'AT' => 'AA' } ] },
    { 'E3000E' => [ { 'CO' => '123' }, { 'AT' => '00' } ] },
    { 'IWWAST' => [ { 'CO' => 'ABC' }, { 'AT' => 'AA' } ] },
    { 'UXWAGL' => [ { 'CO' => 'ABC' }, { 'AT' => 'EF' } ] },
    { 'E3000E' => [ { 'CO' => '123' }, { 'AT' => 'CC' } ] },
    { 'IWWAST' => [ { 'CO' => 'AAA' }, { 'AT' => 'AB' } ] },
);

# Keyword: Schwartzian Transform
my @sorted =
  map { $_->[0] }
    sort { $a->[1] cmp $b->[1] }
      map { [ $_, keys %$_ ] }
        @unsorted;


require Data::Dumper;
print Data::Dumper->new( [ \@sorted ], [ 'sorted' ] )->Maxdepth(2)->Dump();
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Sort Array of Hashes by Key