Sorting Files in Directory.

hashaam83

New member
Joined
Jul 27, 2005
Messages
1
Programming Experience
5-10
I have got a complete working solution for sorting files in a directory but my problem still seems to be unsolved.

The files I have in a directory are as follows:

1.jpg
2.jpg
3.jpg
10.jpg
11.jpg
20.jpg

In folder view of Windows XP, I can clearly see the sorted files, but when I get the files in VB.NET programmatically, it is sorted as follows:

1.jpg
10.jpg
11.jpg
2.jpg
20.jpg
3.jpg

Note: All 1's are sorted together, then 2's and the pattern is followed.

Well, I need a solution which can sort in a correct manner, like I listed in the 1st list.

Any help would be appreciated.
Thanks in advance.

hashaam83
 
File names are strings and consequently get sorted "alphabetically" in most cases. Windows XP has introduced a setting that allows you to sort numerically named files numerically instead, but that is Windows Explorer doing that for you. The underlying file system still orders then alphabetically, and if you deselect the option in Windows then you will see that reflected. To say that one is "correct" and the other is not can only be based on what each individual situation requires. If you want that functionality then you need to provide it yourself. You would need to remove the extension of each file, turn the name into a number and then sort those numbers. If all your files are named like this then that's easy, but you may need to allow for names that contain other characters as well.
 
I just re-read your post up top. Don't know what I though this would help you but my posts were pointless. They don't answer your question. Sorry, I am thinking on it for you though. It's just a puzzle.
 
Last edited:
If the file names are all numeric and you are sure there wil be no letters then just read out all the files into a new array. Then use the String.Remove() function to get rid of the file extension and place the file names into a new array. Bubble sort the new array by converting the strings to integers. As you sort the new array sort the original one as well along with it. Then read back the original array. It should be sorted numerically now.
 
Assuming the file names are as they were originally portrayed, here's how you'd do it:
VB.NET:
Private Function GetFilePathsSortedNumerically(ByVal folder As String) As String()
    Dim filePaths As String() = IO.Directory.GetFiles(folder)
    Dim upperBound As Integer = filePaths.GetUpperBound(0)
    Dim fileNumbers(upperBound) As Integer
    Dim fileNumber As Integer

    For index As Integer = 0 To upperBound Step 1
        Integer.TryParse(IO.Path.GetFileNameWithoutExtension(filePaths(index)), fileNumber)
        fileNumbers(index) = fileNumber
    Next index

    Array.Sort(fileNumbers, filePaths)
    Return filePaths
End Function
That will place all the file names that don't convert to Integers first. If your file names are more complex then you have to add some logic to parse out the numeric part.
 
Back
Top