本文章是由機器翻譯。 將指標移到文章內的文字上方即可查看原文。 其他資訊。 譯文 原文

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

.NET Framework 4.5

語法

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

```

類型參數

TSource

source 之項目的型別。

TKey

keySelector 所傳回之索引鍵的型別。

source

keySelector

comparer

例外狀況

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
*/

```

平台

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (不支援伺服器核心角色), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心角色，不支援 Itanium)

.NET Framework 並不支援各種平台的所有版本。如需支援版本的清單，請參閱.NET Framework 系統需求