使用.Net如何使用Sort方法反向排序数组,即Z到A?

时间:2022-03-19 07:43:10

Using .Net how do I use the Sort method to sort an Array in reverse i.e. Z to A?

使用.Net如何使用Sort方法反向排序数组,即Z到A?

3 个解决方案

#1


11  

Provide an appropriate element comparer. What C# version do you use? 3 lets you do this:

提供适当的元素比较器。您使用什么C#版本? 3允许你这样做:

Array.Sort(myarray, (a, b) => b.CompareTo(a));

#2


3  

You need to pass a IComparer object or Comparison delegate to the Sort function.

您需要将IComparer对象或比较委托传递给Sort函数。

Here is a sample code from C# 2.0

以下是C#2.0的示例代码

   Array.Sort(array,delegate(string a, string b)
    {
        return b.CompareTo(a);
    });

EDIT: missed the array bit.

编辑:错过了阵列位。

#3


1  

if you use a different comparitor that is the reverse of the standard that would do it.

如果你使用的是与标准相反的不同比较器。

Alternatively sort it normally and then reverse it...

或者正常排序然后反转它......

#1


11  

Provide an appropriate element comparer. What C# version do you use? 3 lets you do this:

提供适当的元素比较器。您使用什么C#版本? 3允许你这样做:

Array.Sort(myarray, (a, b) => b.CompareTo(a));

#2


3  

You need to pass a IComparer object or Comparison delegate to the Sort function.

您需要将IComparer对象或比较委托传递给Sort函数。

Here is a sample code from C# 2.0

以下是C#2.0的示例代码

   Array.Sort(array,delegate(string a, string b)
    {
        return b.CompareTo(a);
    });

EDIT: missed the array bit.

编辑:错过了阵列位。

#3


1  

if you use a different comparitor that is the reverse of the standard that would do it.

如果你使用的是与标准相反的不同比较器。

Alternatively sort it normally and then reverse it...

或者正常排序然后反转它......