本文由机器翻译。若要查看英语原文，请勾选“英语”复选框。 也可将鼠标指针移到文本上，在弹出窗口中显示英语原文。 翻译 英语

# Enumerable.OrderByDescending<TSource, TKey> 方法 (IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)

.NET Framework (current version)

## 语法

```public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer
)
```

#### 参数

source
Type:

keySelector
Type: System.Func<TSource, TKey>

comparer
Type:

IComparer<T> 对键进行比较。

#### 返回值

Type: System.Linq.IOrderedEnumerable<TSource>

IOrderedEnumerable<TElement> 的元素按降序顺序根据某个键进行排序。

TSource

TKey

## 异常

Exception Condition
ArgumentNullException

sourcekeySelectornull

## 示例

```/// <summary>
/// This IComparer class sorts by the fractional part of the decimal number.
/// </summary>
public class SpecialComparer : IComparer<decimal>
{
/// <summary>
/// Compare two decimal numbers by their fractional parts.
/// </summary>
/// <param name="d1">The first decimal to compare.</param>
/// <param name="d2">The second decimal to compare.</param>
/// <returns>1 if the first decimal's fractional part
/// is greater than the second decimal's fractional part,
/// -1 if the first decimal's fractional
/// part is less than the second decimal's fractional part,
/// or the result of calling Decimal.Compare()
/// if the fractional parts are equal.</returns>
public int Compare(decimal d1, decimal d2)
{
decimal fractional1, fractional2;

// Get the fractional part of the first number.
try
{
fractional1 = decimal.Remainder(d1, decimal.Floor(d1));
}
catch (DivideByZeroException)
{
fractional1 = d1;
}
// Get the fractional part of the second number.
try
{
fractional2 = decimal.Remainder(d2, decimal.Floor(d2));
}
catch (DivideByZeroException)
{
fractional2 = d2;
}

if (fractional1 == fractional2)
return Decimal.Compare(d1, d2);
else if (fractional1 > fractional2)
return 1;
else
return -1;
}
}

public static void OrderByDescendingEx1()
{
List<decimal> decimals =
new List<decimal> { 6.2m, 8.3m, 0.5m, 1.3m, 6.3m, 9.7m };

IEnumerable<decimal> query =
decimals.OrderByDescending(num =>
num, new SpecialComparer());

foreach (decimal num in query)
{
Console.WriteLine(num);
}
}

/*
This code produces the following output:

9.7
0.5
8.3
6.3
1.3
6.2
*/

```

## 版本信息

.NET Framework

Silverlight

Windows Phone Silverlight

Windows Phone