Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Custom Sort Array

by poj (Monsignor)
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

Replies are listed 'Best First'.
Re^2: Custom Sort Array
by Laurent_R (Canon) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048982]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-04-23 16:36 GMT
Find Nodes?
    Voting Booth?