Question

In other question (here: Pass an IO.DirectoryInfo property as a parameter to a function?) I asked about how to improve a function to pass a DirectoryInfo property as parametter, the problem is the code only works with ""toplevel"" properties like "Name", "Root", "Drive", etc...

But I would need to use the function like this:

Dim Folders As List(Of IO.DirectoryInfo) = blah bla blah...

For Each folderinfo In Bubble_Sort_List_DirectoryInfo(Folders, Function() New IO.DirectoryInfo("").Name)
    MsgBox(folderinfo.Name)
Next

But I need to use the function like this else:

For Each folderinfo In Bubble_Sort_List_DirectoryInfo(Folders, Function() New IO.DirectoryInfo("").Parent.Name.Length)
    MsgBox(folderinfo.Name)
Next

What is necessary to add/modify in this function to manage the usage of DirectoryInfo properties like "Name.Length" or "Parent.Name.Length"?

Private Shared Function Bubble_Sort_List_DirectoryInfo(list As List(Of IO.DirectoryInfo), _
                                                     exp As Expression(Of Func(Of Object))) _
                                                     As List(Of IO.DirectoryInfo)

    Dim member As MemberExpression = _
        If(TypeOf exp.Body Is UnaryExpression, _
           DirectCast(DirectCast(exp.Body, UnaryExpression).Operand, MemberExpression), _
           DirectCast(exp.Body, MemberExpression))

    Return list.Select(Function(s) New With { _
    Key .OrgStr = s, _
    Key .SortStr = System.Text.RegularExpressions.Regex.Replace( _
                   s.Name, "(\d+)|(\D+)", _
                   Function(m) m.Value.PadLeft( _
                               list.Select(Function(folder) DirectCast(DirectCast(member.Member, PropertyInfo) _
                                                            .GetValue(folder, Nothing), Object).ToString.Length).Max(), _
                                                            If(Char.IsDigit(m.Value(0)), " "c, Char.MaxValue))) _
    }).OrderBy(Function(x) x.SortStr).Select(Function(x) x.OrgStr).ToList

End Function

UPDATE:

This are just some explanations and examples.

Inside a directory of my drive I have some folders with folder-names like these:

80's
90's
2000-2006
2007
2008

In my application I get the folders using the "IO.Directory.GetDirectories" method and I store them into a list of DirectoryInfo()

This is the input list:

Dim Folders As List(Of IO.DirectoryInfo) = _
    IO.Directory.GetDirectories("E:\Música\Canciones", "*", IO.SearchOption.TopDirectoryOnly) _
    .Select(Function(p) New IO.DirectoryInfo(p)).ToList()

...But the "IO" method causes the list contents to be sorted as string sort like this:

2000-2006
2007
2008
80's
90's

My desired output is this:

80's
90's
2000-2006
2007
2008

So after creating the List using the "IO" method I'll need to sort the list contents to get my desired output AND THAT'S EXACTLY WHAT I GET USING THE FUNCTION ABOVE calling the function using the property "Name" as parametter to bubble sort the folders by their Name property, so I get my desired output.

well the problem is I need to use other properties like "Name.Length" and "Parent.Name.Length", but the function only allows the usage of a """TopLevel""" property like "Name", "Parent", etc but not variable properties.

Was it helpful?

Solution

Make a class that implements IComparer:

Public Class MyDirectoryInfoComparer
    Implements IComparer(Of IO.DirectoryInfo)

    Public Function Compare(x As IO.DirectoryInfo, y As IO.DirectoryInfo) As Integer _
        Implements IComparer(Of IO.DirectoryInfo).Compare

        ' x comes before y
        Return -1 ' or any number less than 0

        ' x is the same as y
        Return 0

        ' x comes after y
        Return 1 ' or any number greater than 0
End Function

Create your list:

Dim Folders As List(Of IO.DirectoryInfo) = _
    IO.Directory.GetDirectories("E:\Música\Canciones", "*", IO.SearchOption.TopDirectoryOnly) _
    .Select(Function(p) New IO.DirectoryInfo(p)).ToList()

Then use your comparer class to sort it:

Folders.Sort(New MyDirectoryInfoComparer)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top