Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Sort an array of strings based on two fields

by choroba (Abbot)
on Mar 14, 2013 at 10:18 UTC ( #1023447=note: print w/ replies, xml ) Need Help??


in reply to Sort an array of strings based on two fields

The simple way is to write a code for sort which tells Perl how to compare two elements of the list:

#!/usr/bin/perl use warnings; use strict; use feature qw(say); my @strings = qw(xxxxxx.2012-50.yyyyy xxxxxx.2012-51.yyyyy xxxxxx.2012-52.yyyyy xxxxxx.2013-1.yyyyy xxxxxx.2013-2.yyyyy xxxxxx.2013-3.yyyyy xxxxxx.2013-4.yyyyy xxxxxx.2013-10.yyyyy xxxxxx.2013-11.yyyyy ); my @sorted = sort { my ($ay, $an) = $a =~ /\.([0-9]+)-([0-9]+)\./; my ($by, $bn) = $b =~ /\.([0-9]+)-([0-9]+)\./; $ay <=> $by or $an <=> $bn } @string; say for @sorted;

As this might be rather slow for larger lists, various technics exist to speed it up. One of them is called Orcish Maneuvre:

#!/usr/bin/perl use warnings; use strict; use feature qw(say); my @strings = qw(xxxxxx.2012-50.yyyyy xxxxxx.2012-51.yyyyy xxxxxx.2012-52.yyyyy xxxxxx.2013-1.yyyyy xxxxxx.2013-2.yyyyy xxxxxx.2013-3.yyyyy xxxxxx.2013-4.yyyyy xxxxxx.2013-10.yyyyy xxxxxx.2013-11.yyyyy ); my %cache; my @sorted = sort { ($cache{$a} //= generate_key($a)) cmp ($cache{$b} //= generate_key($b)) } @strings; say for @sorted; sub generate_key { my $s = (split /\./, shift)[1]; my ($year, $number) = split /-/, $s; return $year . sprintf '-%03d', $number; }

Update: Simple solution added.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Sort an array of strings based on two fields
Select or Download Code
Re^2: Sort an array of strings based on two fields
by hdb (Prior) on Mar 14, 2013 at 10:50 UTC
    As a small variant, you can calculate a key by 100*year+month, assuming that month will always be two digit at most...
    my @sorted = sort { ($a =~ /\.([0-9]+)-([0-9]+)\./, 100*$1+$2) <=> ($b =~ /\.([0-9]+)-([0-9]+)\./, 100*$1+$2) } @strings;
    or would it be ok to insert the missing zero?
    map { s/-(\d)\./-0\1./ } @strings; my @sorted = sort @strings;
    (Make sure the regexp does not match elsewhere in your strings.)
      I assumed 3 digits in the second solution. 50 seems too big for a months to me (maybe a week?)
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023447]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-12-22 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (109 votes), past polls