in reply to Re^2: Sorting files by 3 numbers in the name
in thread Sorting files by 3 numbers in the name

We are comparing fixed length strings of digits. The result is the same whether we compare them lexically or numerically. I chose the lexical sort because the strings do not seem to have any numerical significance.

Thanks for supplying the link to the module documentation.

UPDATE: Oops! My comment about no numerical significance is wrong. My comment on the subject in level 6 below applies here as well (as long as all fields are of fixed length). I still prefer the lexical sort, but it is harder to justify.

Bill
  • Comment on Re^3: Sorting files by 3 numbers in the name

Replies are listed 'Best First'.
Re^4: Sorting files by 3 numbers in the name
by tobyink (Canon) on May 27, 2017 at 08:30 UTC

    Untested, but this should pad your numbers with leading zeros, just in case the numbers don't turn out to be fixed length.

    my @sorted_files = sort_by { sprintf( '%012d' x 4, (split /_/, $_)[1,7 +,8,9]) } @files;
Re^4: Sorting files by 3 numbers in the name
by LanX (Saint) on May 27, 2017 at 08:03 UTC
    > We are comparing fixed length strings of digits

    I haven't seen that guaranteed, so I prefer playing safe.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Unfortunately, your suggestion is not much safer. It will help only if the first field is the only one that is not of fixed length.

      Either sort should work with tobyink's suggestion to replace the join with sprintf. You can think of the result as a number formed by multiplying each field by different power of ten and then adding them.

      Bill
        Good spot!

        ... hmm maybe a valid feature request to allow array-refs in sort_by, such that all fields are compared in an or chain.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!