in reply to Sort by -M

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.