在目录中获取文件时按名称排序文件 [英] Sort file by name when get file in directory

查看:80
本文介绍了在目录中获取文件时按名称排序文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何在目录排序中获取文件?



Hi,

How to get the file in directory sorting?

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







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





我的图像保存为xps_0.bmp,xps_1.bmp - xps_21.bmp(数字是动态的。图像可以是0到目录中的任意数量的图像)。但是当它获取文件时,它会插入



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

GalleryImages.Add(ImageInfo.FullName)

,其中包含xps_0.bmp,xps_1.bmp,xps_10.bmp,xps_11.bmp数字xps_2.bmp。文件夹中的图像相应地进行排序。但是当检索时,它会跳跃。



如何在没有进入1,10,11的情况下获取1,2,3,4,5,6的文件,12?

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?

推荐答案

因为排序是字母的。一个简单的解决方法是格式化数字,以使它们按字母顺序排序(例如 000 而不是 0 ,如好 001 而不是 1 ,依此类推,假设最大值 1000 文件夹中的文件)。
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).


你必须写您自己的排序(单击此处),您可以在其中解析名称中的数字并按数字排序。





如果可以,解决这个问题的最简单方法是将图像保存为01,02,03等...然后字符串排序将按预期工作。



问题的原因是以1开头的字符串如10在字符串比较中小于2,因为它以较低的字符串值开头。



类似于你如何订购

黄色

黄石

蛋黄



即使是最黄的一个比蛋黄长,比较y = y,e< o ...
黄色和黄色之间的
,比较为w = w然后< s所以黄色先行。



如果这有帮助,请花点时间接受解决方案。谢谢。
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.


这篇关于在目录中获取文件时按名称排序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆