Here was my first thought (which has problems): Since the sort routine can be arbitrarily complex, you could set the -M time to zero if the file doesn't exist. That way there would be no error in the sort and non-existant files would appear at the beginning of the sort order (could use a huge value and force them to the end of the sort order). One issue that I see is if one of the files goes away while you are sorting. This could cause an unstable sort situation since the "M time" for one of the files could change (go from some normal value to zero) during the sort and affect the comparison function.

As afoken++ points out, a Schwartzian transform would be both more efficient CPU wise as well as prevent the above unstable comparison function while sorting problem. For the Schwartzian transform, you would calculate the M time values only once before the sort commences.

No matter what you do, there is the possibility that in your sorted file name list, some file will not exist when it comes to process that list. I think the best you can do is be aware that some files in the sorted list may not exist after sorting. Adjust your sort comparison function so that no errors can occur during the actual sort and these non-existent files appear at one end or the other of the sort. Of course after you create the [filename, Mtime] array for the Schwartzian transform, you can grep out non-normal Mtime entries. However that will not prevent non-existent files from appearing in the final sorted result - you have to handle that no matter what.

Update: I was going to post some code, but I saw the code by tybalt89++. His code looks fine to me and "jives" with my advice. Be aware that even after the sort, some non-existent files may appear in the sorted list.


In reply to Re: Sort by -M by Marshall
in thread Sort by -M by roperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.