使用Time::Piece
和Schwartzian Transform
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);
歡迎#1。你試過什麼了? – KeepCalmAndCarryOn
這些國際日期,還是北美? – Borodin