views:

234

answers:

3

I need to sort filenames that can have a common root, but are then followed by numbers that are not necessarily padded uniformely; one example is what you obtain when you rename multiple files in Windows.

filenamea (1).txt filenamea (2).txt ... filenamea (10).txt ... filenamea (100).txt ... filenameb.txt ... filenamec (1).txt filenamec (2).txt

and so on...

+1  A: 

One solution is to treat numbers (consecutive sequences of digits) as single letters from the point of view of the lexicographical order.

Then "filenamea (3).txt", "filenamea (20).txt" and "filenamea (100).txt" all have the same length, and end up being sorted in this order, which I understand is what you want (?)

Pascal Cuoq
A: 

This one work really nice: http://www.codeproject.com/KB/recipes/csnsort.aspx but it's in c#.

AareP
+4  A: 

There are already similar questions, I know of Sort on a string that may contain a number and How to implement a natural sort algorithm in C. So you can also look there for more inspiration and help.

Both questions' answers suggest, http://www.davekoelle.com/alphanum.html, which is basically what Pascal Cuoq suggested.

You can also look at the Coding Horror article, where some other algorithms are linked: Sorting for Humans : Natural Sort Order

JPvdMerwe
+1 for AlphaNum works a treat for me(http://www.davekoelle.com/alphanum.html)
Binary Worrier
Thanks for the pointer. I was sure it had a name (I think it's Mac OS I had noticed doing these more than 10 years ago).
Pascal Cuoq
This algorithm is even better than what the OP wanted since it handles numbers within the string as well :)
Matthieu M.