1

在那如何按给定顺序对列表进行排序?

我们已经讨论了如何使用 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 使用相同的命令。重新排列后,$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'
       ];

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

4

1 回答 1

1

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

  1. 对于 %toys 的每个元素,
    1. 对于 %toys 的该元素引用的数组的每个元素,
      1. 如前所示对引用的数组进行排序。

并且请不要使用标有“扰乱人心”的解决方案。

于 2013-02-07T23:08:01.377 回答