如何在不使用LINQ的情况下按降序对FileInfo对象数组进行排序 [英] How to sort array of FileInfo objects in descending order without LINQ

查看:87
本文介绍了如何在不使用LINQ的情况下按降序对FileInfo对象数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须降级我的代码才能在不支持LINQ的NET 2.0上工作。当前,代码使用LINQ通过 FullName 属性对 FileInfo 对象的数组进行排序,如下所示:

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()

如何在不使用2.0.NET Framework的情况下执行相同的排序 OrderByDescending LINQ扩展方法?

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

推荐答案

对没有LINQ的数组进行排序,则可以使用 Array.Sort 共享方法。如果有很多重载,则可以使您自定义数组的排序方式。在这种情况下,我建议两个重载要么是使用 IComparer(Of T)对象的重载,要么是使用<$ c $的重载。 c>比较(Of T)委托。

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.

IComparer(Of T)接口可用于创建包装排序逻辑的可重用类。您可以使用相同的 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

如您所见,我使用的是默认比较 String 类( FullName 属性)内置的逻辑以执行比较。之所以将其降序排列,是因为我将 y.FullName x.FullName 进行比较,而不是将 x.FullName y.FullName

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.

然后,您可以使用该类对 FileInfo 对象的数组进行排序,如下所示:

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

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



使用<$排序c $ c> Comparison(Of T)



如果您不需要在多个地方重复使用排序逻辑,或者在这种情况下,比较逻辑非常简单,因此可以轻松创建 Comparison(Of T)委托以指向嵌入式匿名函数,如下所示:

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)
        )
    )

这篇关于如何在不使用LINQ的情况下按降序对FileInfo对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆