Question

I have to get a directory file list, filtered on multiple extensions...and sorted!

I use this, which is the fastest way I've found to get dir content filtered on multiple extensions:

Dim ext As String() = {"*.jpg", "*.bmp","*png"}
Dim files As String() = ext.SelectMany(Function(f) Directory.GetFiles(romPath, f)).ToArray
Array.Sort(files)

and then use an array sort.

I was wondering (and this is my question ;)) if there would be a way to do the sorting IN the same main line? A kind of:

Dim files As String() = ext.SelectMany(Function(f) Directory.GetFiles(romPath, f).**Order By Name**).ToArray

and, if yes, if I would gain speed doing this instead of sorting the array at the end (but I would do my test and report..as soon as I get a solution!!)? Thanks for your help!!

Was it helpful?

Solution

You can use the OrderBy() Linq extension method, like this:

    Dim ext = {"*.jpg", "*.bmp", "*png"}
    Dim files = ext.SelectMany(Function(f) Directory.GetFiles(romPath, f)). _
                OrderBy(Function(f) f). _
                ToArray()

It won't make any difference for speed, sorting is inherently O(nlog(n)) complexity. It does make a diffence in storage, OrderBy() has O(n) storage requirement. Array.Sort() sorts in-place. Not a big deal for small n values, like you'd expect on a disk directory.

OTHER TIPS

enter code hereIf Count = 4 Then
        MsgBox("done")
    ElseIf Count = 0
        Dim aryFi As IO.FileInfo() = (di.GetFiles("*.mp4", IO.SearchOption.AllDirectories))
        For Each fi In aryFi
            Dim ico As Icon = Icon.ExtractAssociatedIcon(fi.FullName)
            Dim imagelistsmall As New ImageList()
            Dim item As New ListViewItem(fi.FullName)
            Dim li As ListViewItem
            Try
                li = ListView1.Items.Add(fi.Name, ImageList1.Images.Count)
                li.Tag = fi.FullName
                ImageList1.Images.Add(Bitmap.FromFile(fi.FullName))

                ListView1.LargeImageList = ImageList1
                ListView1.View = View.List
                Me.Controls.Add(ListView1)

            Catch ex As Exception

            End Try
        Next
        Count = (Count.ToString + 1)
        sack()

    ElseIf Count = 1
        Dim aryFi2 As IO.FileInfo() = (di.GetFiles("*.mov", IO.SearchOption.AllDirectories))
        For Each fi In aryFi2

            Dim ico As Icon = Icon.ExtractAssociatedIcon(fi.FullName)
            Dim imagelistsmall As New ImageList()
            Dim item As New ListViewItem(fi.FullName)
            Dim li As ListViewItem
            Try
                li = ListView1.Items.Add(fi.Name, ImageList1.Images.Count)
                li.Tag = fi.FullName
                ImageList1.Images.Add(Bitmap.FromFile(fi.FullName))

                ListView1.LargeImageList = ImageList1
                ListView1.View = View.List
                Me.Controls.Add(ListView1)

            Catch ex As Exception

            End Try
        Next
        Count = (Count.ToString + 1)
        sack()
    ElseIf Count = 2
        Dim aryFi3 As IO.FileInfo() = (di.GetFiles("*.flv", IO.SearchOption.AllDirectories))
        For Each fi In aryFi3

            Dim ico As Icon = Icon.ExtractAssociatedIcon(fi.FullName)
            Dim imagelistsmall As New ImageList()
            Dim item As New ListViewItem(fi.FullName)
            Dim li As ListViewItem
            Try
                li = ListView1.Items.Add(fi.Name, ImageList1.Images.Count)
                li.Tag = fi.FullName
                ImageList1.Images.Add(Bitmap.FromFile(fi.FullName))

                ListView1.LargeImageList = ImageList1
                ListView1.View = View.List
                Me.Controls.Add(ListView1)

            Catch ex As Exception

            End Try
        Next
        Count = (Count.ToString + 1)
        sack()
    ElseIf Count = 3
        Dim aryFi4 As IO.FileInfo() = (di.GetFiles("*.avi", IO.SearchOption.AllDirectories))
        For Each fi In aryFi4

            Dim ico As Icon = Icon.ExtractAssociatedIcon(fi.FullName)
            Dim imagelistsmall As New ImageList()
            Dim item As New ListViewItem(fi.FullName)
            Dim li As ListViewItem
            Try
                li = ListView1.Items.Add(fi.Name, ImageList1.Images.Count)
                li.Tag = fi.FullName
                ImageList1.Images.Add(Bitmap.FromFile(fi.FullName))

                ListView1.LargeImageList = ImageList1
                ListView1.View = View.List
                Me.Controls.Add(ListView1)

            Catch ex As Exception

            End Try
        Next
        Count = (Count.ToString + 1)
        sack()
    End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top