Sorting an array alphabetically instead of numerically

Hi Folks,

I’ve got a string array that contains items that I wish to sort numerically instead of alphabetically. Using theArray.sort results in the usual:

myfile_1 myfile_10 myfile_11 myfile_12 myfile_2 myfile_3 ...

Of course, what I’m looking for is the natural sort order of _1 through _12.

Does anyone have a natural sort order method / extension? I thought that there was one in Joe Strout’s old StringUtil package, but I can’t find it now.

Charles used to have some sort routines
But his decalresub site seems to have disappeared :frowning:

I have an old copy - mail me off list

For this one case, I’d extract the numbers of each item into an Integer array, then use SortWith. For more flexibility, I’d use a regex to find and pad the numbers in the string and put those into another array, then use SortWith. Using your example:

Original    SortArray
myfile_1    myfile_0000000001
myfile_10   myfile_0000000010
myfile_11   myfile_0000000011
…

You are a regex monster! Sometimes, the simplest mechanisms escape us due to their obviousness :slight_smile: .