sorting - sort strings in perl according to date -


i have set of strings:

$str1: 7-10-2013- x1 $str2: 19-04-2010-g2 $str3: 7-10-2013-x2 $str4: 7-12-2013-a 

i want sort strings according date , alphabet in end. so, above strings after sorting be:

$str2: 19-04-2010-g2 $str1: 7-10-2013-x1 $str3: 7-10-2013-x2 $str4: 7-12-2013-a 

my idea of doing regex grouping , sort according each group. looking more efficient ideas implement in perl. thanks.

using schwartzian transform , fact dates in yyyymmdd format sort lexicographically:

#!/usr/bin/perl use warnings; use strict;  @strings = qw(7-10-2013-x1 19-04-2010-g2 7-10-2013-x2 7-12-2013-a);  print "$_\n" map $_->[1],                  sort { $a->[0] cmp $b->[0] }                  map {                      ($d, $m, $y, $str) = split /-/;                      [sprintf('%d%02d%02d%s', $y, $m, $d, $str), $_]                  }                  @strings; 

Comments