Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Custom Sort Array

by Laurent_R (Vicar)
on Aug 10, 2013 at 21:36 UTC ( #1048948=note: print w/ replies, xml ) Need Help??


in reply to Custom Sort Array

You are not saying everything at once, this is the cause of misunderstandings.

You only need to amend slightly the Schwartzian Transform solution by 2teez to accomodate the new rules.

use strict; use warnings; my @files = map { $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2]} map { [ $_, /\.(\d{12})\-/, /.+?-(\d+)\..+$/ ] } <DATA>; print $_,$/ for @files; __DATA__ abcd1_abc_123456.abc1a_A.201307290800-0900-0.gz abcd1_abc_123456.abc1a_A.201307290800-0900-1.gz abcd1_abc_123456.abc1a_A.201307290800-0900-10.gz abcd1_abc_123456.abc1a_A.201307290800-0900-11.gz abcd1_abc_123456.abc1a_A.201307290800-0900-2.gz abcd1_abc_123456.abc1a_A.201307290800-0900-3.gz abcd1_abc_123456.abc1a_A.201306290800-0900-0.gz abcd1_abc_123456.abc1a_A.201306290800-0900-1.gz abcd1_abc_123456.abc1a_A.201306290800-0900-10.gz abcd1_abc_123456.abc1a_A.201305290800-0900-11.gz abcd1_abc_123456.abc1a_A.201308290800-0900-2.gz abcd1_abc_123456.abc1a_A.201302290800-0900-3.gz

Output:

$ perl schwartz.pl abcd1_abc_123456.abc1a_A.201302290800-0900-3.gz abcd1_abc_123456.abc1a_A.201305290800-0900-11.gz abcd1_abc_123456.abc1a_A.201306290800-0900-0.gz abcd1_abc_123456.abc1a_A.201306290800-0900-1.gz abcd1_abc_123456.abc1a_A.201306290800-0900-10.gz abcd1_abc_123456.abc1a_A.201307290800-0900-0.gz abcd1_abc_123456.abc1a_A.201307290800-0900-1.gz abcd1_abc_123456.abc1a_A.201307290800-0900-2.gz abcd1_abc_123456.abc1a_A.201307290800-0900-3.gz abcd1_abc_123456.abc1a_A.201307290800-0900-10.gz abcd1_abc_123456.abc1a_A.201307290800-0900-11.gz abcd1_abc_123456.abc1a_A.201308290800-0900-2.gz

I guess that's what you need.

Update 21:42 UTC: 2teez was faster than me by about 8 minutes producing a new solution in accordance with the new rules.

Update 2 22:15 UTC: you had a third additional requirement (also use the 4-digit group after the date for the sort) that I had not seen. I've just posted a newly amended version in my answer below.


Comment on Re: Custom Sort Array
Select or Download Code
Re^2: Custom Sort Array
by Laurent_R (Vicar) on Aug 10, 2013 at 22:14 UTC

    I had not read carefully enough your post where you were also asking the files to be sorted according to the group of four digits after the date. It would be far better if you gave all your sorting rules from the outset.

    This a new amendment to take this rule into account:

    use strict; use warnings; my @files = map { $_->[0] } sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2] } map { [ $_, /\.(\d{12}\-\d{4})/, /.+?-(\d+)\..+$/ ] } <DAT +A>; print $_,$/ for @files; __DATA__ abcd1_abc_123456.abc1a_A.201307290800-0900-0.gz abcd1_abc_123456.abc1a_A.201307290800-0900-1.gz abcd1_abc_123456.abc1a_A.201307290800-0900-10.gz abcd1_abc_123456.abc1a_A.201307290800-0900-11.gz abcd1_abc_123456.abc1a_A.201307290800-0900-2.gz abcd1_abc_123456.abc1a_A.201307290800-0900-3.gz abcd1_abc_123456.abc1a_A.201306290800-0900-0.gz abcd1_abc_123456.abc1a_A.201306290800-0900-1.gz abcd1_abc_123456.abc1a_A.201306290800-0900-10.gz abcd1_abc_123456.abc1a_A.201305290800-0900-11.gz abcd1_abc_123456.abc1a_A.201308290800-0900-2.gz abcd1_abc_123456.abc1a_A.201302290800-0900-3.gz abcd1_abc_123456.abc1a_A.201306290800-1000-1.gz abcd1_abc_123456.abc1a_A.201306290800-1000-10.gz abcd1_abc_123456.abc1a_A.201305290800-1000-11.gz abcd1_abc_123456.abc1a_A.201308290800-1000-2.gz abcd1_abc_123456.abc1a_A.201302290800-1000-3.gz
      This is exactly what I was looking for, thank you!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2014-07-24 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (159 votes), past polls