3

I have to downgrade my code to be able to work on NET 2.0, which does not support LINQ. Currently, the code sorts an array of FileInfo objects by their FullName property, using LINQ, like this:

    Dim files As FileInfo() = ' ...
    files = files.OrderByDescending(Function(x) x.FullName).ToArray()

How can I perform the same kind of sort in the 2.0 .NET Framework without using the OrderByDescending LINQ extension method?

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
Wine Too
  • 4,515
  • 22
  • 83
  • 137
  • `OrderByDescending` and `Where` are LINQ extension methods, so they will not work in the 2.0 framework. Which of those two method are you having trouble with? What have you tried? – Steven Doggart Feb 28 '13 at 13:38
  • Hi Steven. Yes I should get rid of Linq. I updated my question where you can see that only sorting of "files" in fileinfo by name - descending remains to get my function finished. – Wine Too Feb 28 '13 at 13:46
  • 1
    Perhaps this will help...http://stackoverflow.com/questions/52842/sorting-directory-getfiles. – aphoria Feb 28 '13 at 14:24

1 Answers1

4

To sort an array without LINQ, you can use the Array.Sort shared method. If has many overloads that allow you to customize the way the array is sorted. The two overloads that I would recommend, in this case, would be either the one that takes an IComparer(Of T) object, or the one that takes a Comparison(Of T) delegate.

Sort Using IComparer(Of T)

The IComparer(Of T) interface can be used to create re-usable classes that wrap the sorting logic. You can easily sort any array or list using the same IComparer(Of T) class, so it is often more convenient in situations where you need to re-use the same sorting logic in multiple places in your code. First, you need to create the class that implements the interface, like this:

Public Class FileInfoDescendingByNameComparer
    Implements IComparer(Of FileInfo)

    Public Function Compare(x As FileInfo, y As FileInfo) As Integer Implements IComparer(Of FileInfo).Compare
        Return y.FullName.CompareTo(x.FullName)
    End Function
End Class

As you can see, I am using the default comparison logic built into the String class (the FullName property) to perform the comparison. The reason it will sort in descending order is because I am comparing y.FullName to x.FullName rather than comparing x.FullName to y.FullName.

Then, you can sort the array of FileInfo objects using that class, like this:

Array.Sort(Of FileInfo)(files, New FileInfoDescendingByNameComparer())

Sort Using Comparison(Of T)

If you don't need to re-use the sorting logic in multiple places, or, as in this case, the comparison logic is very simple, it is easier to create a Comparison(Of T) delegate to point to an in-line anonymous function, like this:

Array.Sort(Of FileInfo) _
    ( 
    files, 
    New Comparison(Of FileInfo) _
        ( 
        Function(f1 As FileInfo, f2 As FileInfo) f2.FullName.CompareTo(f1.FullName)
        )
    )
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Anonymous function in .NET 2.0? – Victor Zakharov Feb 28 '13 at 14:32
  • 2
    @Neolisk Yes. Anonymous functions, curiously enough, are actually supported when targeting the .NET 2.0 framework. It's not supported by older versions of Visual Studio, but as long as you are using a newer version of Visual Studio, it will handle it properly. At least it does in 2012, anyway. – Steven Doggart Feb 28 '13 at 14:36
  • 1
    This is interesting. I was absolutely sure it needs at least .NET 3.0, for some reason. Just tested under VS 2008 and 2010, both support this feature when targeting .NET 2.0. +1 for the info. – Victor Zakharov Feb 28 '13 at 14:42
  • Thank you very much Steven. Both ways work as expected, inline and class! – Wine Too Feb 28 '13 at 14:42