my %m; @allPictures = sort { ($m{$a} ||= -M $a) <=> ($m{$b} ||= -M $b) } @allPictures;