以降序排序数组的更好方法

时间:2022-11-06 07:44:21

I have a array of int which I have to sort by descending.

我有一个int数组,我必须通过降序排序。

Since I did not find any method to sort the array in descending order.Currently I am sorting the array in descending order as below

因为我没有找到任何方法来按降序对数组进行排序。目前我按降序对数组进行排序,如下所示

int[] array = new int[] { 3, 1, 4, 5, 2 };
Array.Sort<int>( array );
Array.Reverse( array );

Now,the question is that.Is there any better way to do the same in c#?

现在,问题是。在c#中有没有更好的方法来做同样的事情?

5 个解决方案

#1


51  

Depending on the sort order, you can do this :

根据排序顺序,您可以执行以下操作:

    int[] array = new int[] { 3, 1, 4, 5, 2 };
    Array.Sort<int>(array,
                    new Comparison<int>(
                            (i1, i2) => i2.CompareTo(i1)
                    ));

... or this :

... 或这个 :

    int[] array = new int[] { 3, 1, 4, 5, 2 };
    Array.Sort<int>(array,
                    new Comparison<int>(
                            (i1, i2) => i1.CompareTo(i2)
                    ));

i1 and i2 are just reversed.

i1和i2刚刚颠倒过来。

#2


52  

Use LINQ OrderByDescending method. It returns IOrderedIEnumerable<int>, which you can convert back to Array if you need so. Generally, List<>s are more functional then Arrays.

使用LINQ OrderByDescending方法。它返回IOrderedIEnumerable ,如果需要,可以将其转换回Array。通常,List <> s比Arrays更具功能性。

array = array.OrderByDescending(c => c).ToArray();

#3


9  

Sure, You can customize the sort.

当然,您可以自定义排序。

You need to give the Sort() a delegate to a comparison method which it will use to sort.

您需要将Sort()作为委托给它将用于排序的比较方法。

Using an anonymous method:

使用匿名方法:

Array.Sort<int>( array,
delegate(int a, int b)
  {
    return b - a; //Normal compare is a-b
  }); 

Read more about it:

阅读更多相关信息:

Sorting arrays
MSDN - Array.Sort Method (T[], Comparison)

排序数组MSDN - Array.Sort方法(T [],比较)

#4


2  

Yes, you can pass predicate to sort. That would be your reverse implementation.

是的,您可以将谓词传递给排序。那将是你的反向实现。

#5


2  

You may specify a comparer(IComparer implementation) as a parameter in Array.Sort, the order of sorting actually depends on comparer. The default comparer is used in ascending sorting

您可以将比较器(IComparer实现)指定为Array.Sort中的参数,排序顺序实际上取决于comparer。默认比较器用于升序排序

#1


51  

Depending on the sort order, you can do this :

根据排序顺序,您可以执行以下操作:

    int[] array = new int[] { 3, 1, 4, 5, 2 };
    Array.Sort<int>(array,
                    new Comparison<int>(
                            (i1, i2) => i2.CompareTo(i1)
                    ));

... or this :

... 或这个 :

    int[] array = new int[] { 3, 1, 4, 5, 2 };
    Array.Sort<int>(array,
                    new Comparison<int>(
                            (i1, i2) => i1.CompareTo(i2)
                    ));

i1 and i2 are just reversed.

i1和i2刚刚颠倒过来。

#2


52  

Use LINQ OrderByDescending method. It returns IOrderedIEnumerable<int>, which you can convert back to Array if you need so. Generally, List<>s are more functional then Arrays.

使用LINQ OrderByDescending方法。它返回IOrderedIEnumerable ,如果需要,可以将其转换回Array。通常,List <> s比Arrays更具功能性。

array = array.OrderByDescending(c => c).ToArray();

#3


9  

Sure, You can customize the sort.

当然,您可以自定义排序。

You need to give the Sort() a delegate to a comparison method which it will use to sort.

您需要将Sort()作为委托给它将用于排序的比较方法。

Using an anonymous method:

使用匿名方法:

Array.Sort<int>( array,
delegate(int a, int b)
  {
    return b - a; //Normal compare is a-b
  }); 

Read more about it:

阅读更多相关信息:

Sorting arrays
MSDN - Array.Sort Method (T[], Comparison)

排序数组MSDN - Array.Sort方法(T [],比较)

#4


2  

Yes, you can pass predicate to sort. That would be your reverse implementation.

是的,您可以将谓词传递给排序。那将是你的反向实现。

#5


2  

You may specify a comparer(IComparer implementation) as a parameter in Array.Sort, the order of sorting actually depends on comparer. The default comparer is used in ascending sorting

您可以将比较器(IComparer实现)指定为Array.Sort中的参数,排序顺序实际上取决于comparer。默认比较器用于升序排序