Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^2: Custom Sort Array

by Laurent_R (Abbot)
on Aug 11, 2013 at 09:05 UTC ( #1048988=note: print w/replies, xml ) Need Help??

in reply to Re: Custom Sort Array
in thread Custom Sort Array

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

The output is as follows:

$ perl abcd1_abc_123456.abc1a_A.201302290800-0900-3.gz abcd1_abc_123456.abc1a_A.201302290800-1000-3.gz abcd1_abc_123456.abc1a_A.201305290800-0900-11.gz abcd1_abc_123456.abc1a_A.201305290800-1000-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.201306290800-1000-1.gz abcd1_abc_123456.abc1a_A.201306290800-1000-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 abcd1_abc_123456.abc1a_A.201308290800-1000-2.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://1048988]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2016-10-23 12:10 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (301 votes). Check out past polls.