开发者

VB.NET Sort files in directory by alphanumeric

开发者 https://www.devze.com 2023-04-09 08:02 出处:网络
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

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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号