Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Custom Sort Array

by poj (Priest)
on Aug 11, 2013 at 07:04 UTC ( #1048982=note: print w/ replies, xml ) Need Help??


in reply to Custom Sort Array

If you have a maximum sequence number of -99 then try this ;

#!perl use strict; chomp( my @files = <DATA>) ; # add leading zero for 0 to 9 s/-(\d)(\.gz)$/-0$1$2/ for @files; # sort my @sorted = sort @files; # remove leading zero for 00 to 09 s/-0(\d)(\.gz)$/-$1$2/ for @sorted; print "$_\n" for @sorted; __DATA__ abcd1_abc_123456.abc1a_A.201307290800-0900-0.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-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.201307280800-0900-0.gz abcd1_abc_123456.abc1a_A.201307280800-0900-10.gz abcd1_abc_123456.abc1a_A.201307280800-0900-11.gz abcd1_abc_123456.abc1a_A.201307280800-0900-2.gz abcd1_abc_123456.abc1a_A.201307280800-0900-3.gz
poj


Comment on Re: Custom Sort Array
Select or Download Code
Re^2: Custom Sort Array
by Laurent_R (Parson) on Aug 11, 2013 at 09:05 UTC

    poj's idea of changing the words to enable lexicographical sort can be expressed as a variant of the Schwartzian Transform (ST) called the Guttman Rosler Transform (GRT), which is deemed to be faster than the ST because the sort phase is entirely C code and it avoids one level of indirection. The idea of poj expressed with the GRT construct:

    use strict; use warnings; my @files = map {s/-0(\d)(\.gz)$/-$1$2/; $_} sort map {s/-(\d)(\.gz)$/-0$1$2/; $_} <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 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

    For more information on the GRT, see e.g. Advanced Sorting - GRT - Guttman Rosler Transform (this is just one I picked up at random, you have several links in this post to other nodes, and there are many other nodes on the subject)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2014-12-22 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (119 votes), past polls