本文整理汇总了C#中System.Linq.Enumerable.OrderByDescending方法的典型用法代码示例。如果您正苦于以下问题:C# Enumerable.OrderByDescending方法的具体用法?C# Enumerable.OrderByDescending怎么用?C# Enumerable.OrderByDescending使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Enumerable.OrderByDescending方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compare
/// <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);
}
}
输出:
9.7 0.5 8.3 6.3 1.3 6.2