Listing files in numerical order instead of alphabetical? Listing files in numerical order instead of alphabetical? unix unix

Listing files in numerical order instead of alphabetical?


You could put them in an array and sort the array with the natsort­Docs function:

$array = array('logo1','logo2','logo12');natsort($array);

Which gives (Demo):

array(3) {  [0]=>  string(5) "logo1"  [1]=>  string(5) "logo2"  [2]=>  string(6) "logo12"}

The order you're looking for is often called natural order.

Alternatively, you could prefix the numbers, e.g. if you're already using sprintf to name the files, so that the standard sort order would still work:

`logo%03d.jpg`

Which would generate

logo001.jpg

for decimal 1.


If you're using ls like you say...

ls | sort -n

will do the trick.