Perl - 如何按给定顺序对数组值进行排序?

时间:2013-02-07 21:51:42

标签: perl hashtable

How to sort a list with a given order?

我们已经讨论过如何使用map和$ _基于给定的顺序对列表进行排序。今天我还有另一个问题。

我有相同的命令:

my @orderby = ( 'car', 'boat', 'chicken', 'cat', 'dog', 'mouse');
# or if it's better to the code:
my %orderby = ( 'car' => 0, 
                'boat' => 1, 
                'chicken' => 2, 
                'cat' => 3, 
                'dog' => 4, 
                'mouse' => 5); 

现在我有以下需要按orderby排序:

print Dumper \%toys;
$VAR = {
         'animals'  =>  [
                          [
                            'feather', 'cluck-2', 'chicken', 'white'
                          ],
                          [
                            'bald', 'bark', 'dog', 'black stripes'
                          ],
                          [
                            'feather', 'cluck-2', 'chicken', 'white'
                          ]
                        ],
         'notanima' =>  [
                          [
                            'paited', 'motor', 'boat', 'red'
                          ],
                          [
                            'painted', 'motor', 'car', 'blue on top'
                          ]
                        ]
       };

代码需要使用基于orderby的3列进行排序。你需要对动物和notanima使用相同的ordery。 重新排列后,$ VAR将是:

$VAR = {
         'animals'  =>  [
                          [
                            'feather', 'cluck-2', 'chicken', 'white'
                          ],
                          [
                            'feather', 'cluck-2', 'chicken', 'white'
                          ],
                          [
                            'bald', 'bark', 'dog', 'black stripes'
                          ]
                        ],
         'notanima' =>  [
                          [
                            'painted', 'motor', 'car', 'blue on top'
                          ],
                          [
                            'paited', 'motor', 'boat', 'red'
                          ]
                        ]
       }; 
order %toys{key} by orderby;

我试图更改@ikegami提供的地图解决方案

my %counts; ++$counts{$_} for @list;
my @sorted = map { ($_) x ($counts{$_}||0) } @orderby;

但我没有成功。 你们有什么想法我怎样才能实现这个目标?

提前谢谢!

更新!
我试图使用ikegami的建议,我已经这样做了:

# that first foreach will give one ARRAY for animals and one ARRAY for notanima
foreach my $key (keys %toys)
{
   # that one will give me access to the ARRAY referenced by the $key.
   foreach my $toy_ref ($toys{$key})
   {
       my %orderby = map {$orderby[$_] => $_} 0..$#orderby;
       my @sorted = sort { $orderby{$a} <=> $orderby{$b} } @{$toy_ref};
       # my @sorted = sort { $orderby{$a} <=> $orderby{$b} } $toy_ref;
       print Dumper @sorted;
   }
}

首先,这给了我警告:

Use of uninitialized value in numeric comparison (<=>) at....

对于notanima的排序结果(我会忽略动物,所以帖子不会那么大):

$VAR1 = [
         'paited', 'motor', 'boat', 'red'
       ];
$VAR2 = [
         'painted', 'motor', 'car', 'blue on top'
       ];

根据orderby,打印顺序必须为:

$VAR1 = [
         'painted', 'motor', 'car', 'blue on top'
       ];
$VAR2 = [
         'paited', 'motor', 'boat', 'red'
       ];

汽车需要先来。 我做错了什么?

1 个答案:

答案 0 :(得分:1)

所以你有6个数组要排序,所以你需要六次调用sort

  1. 对于%玩具的每个元素,
    1. 对于%toys的元素引用的数组的每个元素,
      1. 按先前显示的方式对引用的数组进行排序。
  2. 请不要使用标有“与人们的思想混淆”的解决方案。

相关问题