MATLAB: Sorting alphabetically with sortrows: underscore handled differently than in windows

alphabeticallydifferentlyfilenameMATLABsortingsortrows

Hello everybody,
I am having a problem with the sortrows. I am sorting filenames alphabetically but I get a different sorted list than what Windows shows in the Explorer.
Here is how I sort with Matlab:
>> sorted = sortrows({'abc1_test.txt'; 'abc11_test.txt'})
sorted =
'abc11_test.txt'
'abc1_test.txt'
The Matlab display of the current folder and the Windows Explorer show a different sorting:
files_matlab.PNG
So for some reason, sortrows prefers numbers over underscore while Windows and even the Matlab display have it vice versa.
Is this a bug? What can I do to get the same sorting result in my Matlab scripts?
Regards
Stephan

Best Answer

If you actually want to sort filenames into alphanumeric order, then one option is to download my FEX submission natsortfiles:
and then use it something like this:
>> C = {'abc1_test.txt'; 'abc11_test.txt'};
>> D = natsortfiles(C)
D =
'abc1_test.txt'
'abc11_test.txt'
Related Question