Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

How to get the file in directory sorting?

VB
Dim ImageInfo As FileInfo
Dim imgBrush As New ImageBrush
Dim ImageMediaDirectory As New DirectoryInfo(path)


VB
Dim FileCount As Integer = Directory.GetFiles(path).Length
If FileCount > 0 Then
     For Each ImageInfo In ImageMediaDirectory.GetFiles("*.*")
         GalleryImages.Add(ImageInfo.FullName)
     Next
End If


My images is save as xps_0.bmp,xps_1.bmp - xps_21.bmp(number is dynamic. Images can be 0,1 to any number of images in directory). But when it get files, it insert into
VB
GalleryImages.Add(ImageInfo.FullName)
with xps_0.bmp, xps_1.bmp, xps_10.bmp, xps_11.bmp which jump the number xps_2.bmp. The images in the folder is sorting accordingly. But when retrieve, it jumps.

How can it get the file with sorting 1,2,3,4,5,6 without goes to 1,10,11,12?
Posted

Because the sort is alphabetic. A simple workaround would be formatting the number in order to make them properly alphabetically ordered (e.g. 000 instead of 0, as well 001 instead of 1, and so on, assuming a maximum of 1000 files in the folder).
 
Share this answer
 
Comments
Luiey Ichigo 4-Feb-15 3:16am    
thanks..manage to deal with it as your advise. When saving the filename into database, I format it to string as:-

strName = pageCount.ToString("0000")

And it's work. Thanks for advise
CPallini 4-Feb-15 5:38am    
You are welcome.
You have to write your own sort (click here) in which you parse the number out of the name and sort by the number.

BUT
If you can, easiest way to get around this problem is to save the images as 01, 02, 03 etc...then string sort will work as expected.

The cause of the problem is that string starting with 1 like "10" is in string comparison smaller then "2" since it starts with "lower" string value.

Similar to how you order
yellow
yellowstone
yolk

even if yellowstone is longer then yolk, it is compared y = y, e < o...
between yellow and yellowstone, the comparison goes to w = w and then "" < "s" so yellow goes first.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900