|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
Enumerable.OrderByDescending<TSource, TKey>, méthode (IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)
Espace de noms : System.Linq
Assembly : System.Core (dans System.Core.dll)
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer )
Paramètres de type
- TSource
Type des éléments de source.
- TKey
Type de clé retournée par keySelector.
Paramètres
- source
- Type : System.Collections.Generic.IEnumerable<TSource>
Séquence de valeurs à classer.
- keySelector
- Type : System.Func<TSource, TKey>
Fonction permettant d'extraire une clé d'un élément.
- comparer
- Type : System.Collections.Generic.IComparer<TKey>
IComparer<T> pour comparer les clés.
Valeur de retour
Type : System.Linq.IOrderedEnumerable<TSource>Remarque sur l'utilisation
En Visual Basic et C#, vous pouvez appeler cette méthode comme une méthode d'instance sur n'importe quel objet de type IEnumerable<TSource>. Lorsque vous utilisez la syntaxe des méthodes d'instance pour appeler cette méthode, omettez le premier paramètre. Pour plus d'informations, consultez Méthodes d'extension (Visual Basic) ou Méthodes d'extension (Guide de programmation C#).| Exception | Condition |
|---|---|
| ArgumentNullException |
Remarque |
|---|
/// <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 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (rôle principal du serveur non pris en charge), Windows Server 2008 R2 (rôle principal du serveur pris en charge avec SP1 ou version ultérieure ; Itanium non pris en charge)
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.
Remarque