VB.NET Sort files in directory by alphanumeric

匿名 (未验证) 提交于 2019-12-03 09:52:54

问题:

How do I sort the files in this directory below by alphanumeric? An example of a file: 12325_2011.jpg

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load     If Not Page.IsPostBack Then         Dim di As New IO.DirectoryInfo(ImagePath)         Dim imageArray As IO.FileInfo() = di.GetFiles()         Dim image As IO.FileInfo          'list the names of all images in the specified directory          For Each image In imageArray             CheckBoxList1.Items.Add(image.Name)         Next     End If End Sub

回答1:

Just modify your existing For Each loop like this:

For Each image In imageArray.OrderBy(Function(i) i.Name)     CheckBoxList1.Items.Add(image.Name) Next


回答2:

You could use the sorted list class instead of your image array:

http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.aspx

e.g.

For each Item in di.GetFiles    'Add image url to sorted list Next   For Each Item in SortedList     'Add to checkbox list Next


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!