简体   繁体   中英

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

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

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

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

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

Here is a sample code from C# 2.0

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

EDIT: missed the array bit.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM