Arrays 在perl中对日期数组进行排序并返回索引
我有一系列的日期Arrays 在perl中对日期数组进行排序并返回索引,arrays,perl,sorting,date,indexing,Arrays,Perl,Sorting,Date,Indexing,我有一系列的日期 @dates = qw(2/1/1989 2/1/1970 2/1/1970 2/1/1989 6/1/1970 12/1/1970); 我需要从最早的到最新的排序,并在排序后返回未排序数组的索引 输出应该是这样的 #sorted array 2/1/1970 2/1/1970 6/1/1970 12/1/1970 2/1/1989 2/1/1989 #indexes 1 2 4 5 0 3 使用和 也可以只执行以下操作: my @idx = sort { Ti
@dates = qw(2/1/1989 2/1/1970 2/1/1970 2/1/1989 6/1/1970 12/1/1970);
我需要从最早的到最新的排序,并在排序后返回未排序数组的索引
输出应该是这样的
#sorted array
2/1/1970
2/1/1970
6/1/1970
12/1/1970
2/1/1989
2/1/1989
#indexes
1 2 4 5 0 3
使用和
也可以只执行以下操作:
my @idx = sort {
Time::Piece->strptime($dates[$a], '%m/%d/%Y') <=> Time::Piece->strptime($dates[$b], '%m/%d/%Y')
} (0..$#dates);
my@idx=sort{
时间::计件->打印时间($dates[$a],'%m/%d/%Y')时间::计件->打印时间($dates[$b],'%m/%d/%Y'))
}(0..$#日期);
欢迎来到Stackoverflow。到目前为止你试过什么?这些是国际约会还是北美约会?
indexes 1 2 4 5 0 3
2/1/1970
2/1/1970
6/1/1970
12/1/1970
2/1/1989
2/1/1989
use strict;
use warnings;
use Time::Piece;
my @dates = qw(2/1/1989 2/1/1970 2/1/1970 2/1/1989 6/1/1970 12/1/1970);
my @idx = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [$_, Time::Piece->strptime($dates[$_], '%m/%d/%Y') ] }
(0..$#dates);
print "Indexes: @idx\n";
print "Dates: @dates[@idx]\n";
Indexes: 1 2 4 5 0 3
Dates: 2/1/1970 2/1/1970 6/1/1970 12/1/1970 2/1/1989 2/1/1989
my @idx = sort {
Time::Piece->strptime($dates[$a], '%m/%d/%Y') <=> Time::Piece->strptime($dates[$b], '%m/%d/%Y')
} (0..$#dates);