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

Nice, but you should better choose nsort_by()

From the docs of List::UtilsBy

> Similar to sort_by but compares its key values numerically.

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

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

Replies are listed 'Best First'.
Re^3: Sorting files by 3 numbers in the name
by BillKSmith (Monsignor) on May 27, 2017 at 02:43 UTC

    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

      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;
      > 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