Enumerable.ThenByDescending Methode
Definition
Führt eine nachfolgende Sortierung der Elemente in einer Sequenz in absteigender Reihenfolge durchPerforms a subsequent ordering of the elements in a sequence in descending order.
Überlädt
ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>) |
Führt eine nachfolgende Sortierung der Elemente in einer Sequenz in absteigender Reihenfolge nach einem Schlüssel durchPerforms a subsequent ordering of the elements in a sequence in descending order, according to a key. |
ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) |
Führt mithilfe eines angegebenen Vergleichs eine nachfolgende Sortierung der Elemente in einer Sequenz in absteigender Reihenfolge durchPerforms a subsequent ordering of the elements in a sequence in descending order by using a specified comparer. |
ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)
Führt eine nachfolgende Sortierung der Elemente in einer Sequenz in absteigender Reihenfolge nach einem Schlüssel durchPerforms a subsequent ordering of the elements in a sequence in descending order, according to a key.
public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
static System::Linq::IOrderedEnumerable<TSource> ^ ThenByDescending(System::Linq::IOrderedEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static System.Linq.IOrderedEnumerable<TSource> ThenByDescending<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member ThenByDescending : System.Linq.IOrderedEnumerable<'Source> * Func<'Source, 'Key> -> System.Linq.IOrderedEnumerable<'Source>
<Extension()>
Public Function ThenByDescending(Of TSource, TKey) (source As IOrderedEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As IOrderedEnumerable(Of TSource)
Typparameter
- TSource
Der Typ der Elemente von source
.The type of the elements of source
.
- TKey
Der Typ des von keySelector
zurückgegebenen Schlüssels.The type of the key returned by keySelector
.
Parameter
- source
- IOrderedEnumerable<TSource>
Ein IOrderedEnumerable<TElement> mit den zu sortierenden Elementen.An IOrderedEnumerable<TElement> that contains elements to sort.
- keySelector
- Func<TSource,TKey>
Eine Funktion zum Extrahieren eines Schlüssels aus jedem Element.A function to extract a key from each element.
Gibt zurück
- IOrderedEnumerable<TSource>
Ein IOrderedEnumerable<TElement>, dessen Elemente in absteigender Reihenfolge nach einem Schlüssel sortiert werdenAn IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
Ausnahmen
source
oder keySelector
ist null
.source
or keySelector
is null
.
Hinweise
Diese Methode wird mithilfe von verzögerter Ausführung implementiert.This method is implemented by using deferred execution. Der unmittelbare Rückgabewert ist ein Objekt, das alle Informationen speichert, die zum Ausführen der Aktion erforderlich sind.The immediate return value is an object that stores all the information that is required to perform the action. Die durch diese Methode dargestellte Abfrage wird erst ausgeführt, wenn das Objekt durch Aufrufen der- GetEnumerator
Methode direkt oder mithilfe von foreach
in Visual c# oder in Visual Basic aufgezählt wird For Each
.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator
method directly or by using foreach
in Visual C# or For Each
in Visual Basic.
Um eine Sequenz nach den Werten der Elemente selbst zu sortieren, geben Sie die Identitäts Funktion ( x => x
in Visual c# oder Function(x) x
in Visual Basic) für an keySelector
.To order a sequence by the values of the elements themselves, specify the identity function (x => x
in Visual C# or Function(x) x
in Visual Basic) for keySelector
.
ThenBy und ThenByDescending werden definiert, um den-Typ zu erweitern IOrderedEnumerable<TElement> , der auch der Rückgabetyp dieser Methoden ist.ThenBy and ThenByDescending are defined to extend the type IOrderedEnumerable<TElement>, which is also the return type of these methods. Mit diesem Entwurf können Sie mehrere Sortierkriterien angeben, indem Sie eine beliebige Anzahl von- ThenBy oder- ThenByDescending Methoden anwenden.This design enables you to specify multiple sort criteria by applying any number of ThenBy or ThenByDescending methods.
Hinweis
Da IOrderedEnumerable<TElement> von erbt IEnumerable<T> , können Sie OrderBy oder OrderByDescending für die Ergebnisse eines Aufrufes OrderBy , oder Abrufen OrderByDescending ThenBy ThenByDescending .Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Dadurch wird eine neue primäre Reihenfolge eingeführt, die die zuvor festgelegte Reihenfolge ignoriert.Doing this introduces a new primary ordering that ignores the previously established ordering.
Diese Sortiermethode vergleicht Schlüssel mit dem Standardcomparer Default .This sorting method compares keys by using the default comparer Default.
Diese Methode führt eine stabile Sortierung aus. Das heißt, wenn die Schlüssel zweier Elemente gleich sind, wird die Reihenfolge der Elemente beibehalten.This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. Im Gegensatz dazu behält eine instabile Sortierung nicht die Reihenfolge von Elementen mit demselben Schlüssel bei.In contrast, an unstable sort does not preserve the order of elements that have the same key.
In der Syntax von Visual c#-Abfrage Ausdrücken übersetzt eine- orderby [first criterion], [second criterion] descending
Klausel in einen Aufruf von ThenByDescending .In Visual C# query expression syntax, an orderby [first criterion], [second criterion] descending
clause translates to an invocation of ThenByDescending.
In Visual Basic Abfrage Ausdruckssyntax übersetzt eine- Order By [first criterion], [second criterion] Descending
Klausel in einen Aufruf von ThenByDescending .In Visual Basic query expression syntax, an Order By [first criterion], [second criterion] Descending
clause translates to an invocation of ThenByDescending.
Gilt für:
ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)
Führt mithilfe eines angegebenen Vergleichs eine nachfolgende Sortierung der Elemente in einer Sequenz in absteigender Reihenfolge durchPerforms a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
static System::Linq::IOrderedEnumerable<TSource> ^ ThenByDescending(System::Linq::IOrderedEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer);
public static System.Linq.IOrderedEnumerable<TSource> ThenByDescending<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey> comparer);
public static System.Linq.IOrderedEnumerable<TSource> ThenByDescending<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);
static member ThenByDescending : System.Linq.IOrderedEnumerable<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IComparer<'Key> -> System.Linq.IOrderedEnumerable<'Source>
<Extension()>
Public Function ThenByDescending(Of TSource, TKey) (source As IOrderedEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), comparer As IComparer(Of TKey)) As IOrderedEnumerable(Of TSource)
Typparameter
- TSource
Der Typ der Elemente von source
.The type of the elements of source
.
- TKey
Der Typ des von keySelector
zurückgegebenen Schlüssels.The type of the key returned by keySelector
.
Parameter
- source
- IOrderedEnumerable<TSource>
Ein IOrderedEnumerable<TElement> mit den zu sortierenden Elementen.An IOrderedEnumerable<TElement> that contains elements to sort.
- keySelector
- Func<TSource,TKey>
Eine Funktion zum Extrahieren eines Schlüssels aus jedem Element.A function to extract a key from each element.
- comparer
- IComparer<TKey>
Ein IComparer<T> zum Vergleichen von Schlüsseln.An IComparer<T> to compare keys.
Gibt zurück
- IOrderedEnumerable<TSource>
Ein IOrderedEnumerable<TElement>, dessen Elemente in absteigender Reihenfolge nach einem Schlüssel sortiert werdenAn IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
Ausnahmen
source
oder keySelector
ist null
.source
or keySelector
is null
.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie verwendet ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) wird, um eine sekundäre Sortierung der Elemente in einer Sequenz in absteigender Reihenfolge mithilfe eines benutzerdefinierten Vergleichs auszuführen.The following code example demonstrates how to use ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) to perform a secondary ordering of the elements in a sequence in descending order by using a custom comparer.
public class CaseInsensitiveComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return string.Compare(x, y, true);
}
}
public static void ThenByDescendingEx1()
{
string[] fruits = { "apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE", "apPLE" };
// Sort the strings first ascending by their length and
// then descending using a custom case insensitive comparer.
IEnumerable<string> query =
fruits
.OrderBy(fruit => fruit.Length)
.ThenByDescending(fruit => fruit, new CaseInsensitiveComparer());
foreach (string fruit in query)
{
Console.WriteLine(fruit);
}
}
/*
This code produces the following output:
apPLe
apple
APple
apPLE
orange
ORANGE
baNanA
BAnana
*/
' This class provides a custom implementation of the Compare() method.
Class CaseInsensitiveComparer
Implements IComparer(Of String)
Function Compare(ByVal x As String, ByVal y As String) As Integer _
Implements IComparer(Of String).Compare
' Compare values and ignore case.
Return String.Compare(x, y, True)
End Function
End Class
Sub ThenByDescendingEx1()
Dim fruits() As String =
{"apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE", "apPLE"}
' Sort the strings first by their length and then
' by using a custom "case insensitive" comparer.
Dim query As IEnumerable(Of String) =
fruits _
.OrderBy(Function(fruit) fruit.Length) _
.ThenByDescending(Function(fruit) fruit, New CaseInsensitiveComparer())
' Display the results.
Dim output As New System.Text.StringBuilder
For Each fruit As String In query
output.AppendLine(fruit)
Next
Console.WriteLine(output.ToString())
End Sub
' This code produces the following output:
' apPLe
' apple
' APple
' apPLE
' orange
' ORANGE
' baNanA
' BAnana
Hinweise
Diese Methode wird mithilfe von verzögerter Ausführung implementiert.This method is implemented by using deferred execution. Der unmittelbare Rückgabewert ist ein Objekt, das alle Informationen speichert, die zum Ausführen der Aktion erforderlich sind.The immediate return value is an object that stores all the information that is required to perform the action. Die durch diese Methode dargestellte Abfrage wird erst ausgeführt, wenn das Objekt durch Aufrufen der- GetEnumerator
Methode direkt oder mithilfe von foreach
in Visual c# oder in Visual Basic aufgezählt wird For Each
.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator
method directly or by using foreach
in Visual C# or For Each
in Visual Basic.
Um eine Sequenz nach den Werten der Elemente selbst zu sortieren, geben Sie die Identitäts Funktion ( x => x
in Visual c# oder Function(x) x
in Visual Basic) für an keySelector
.To order a sequence by the values of the elements themselves, specify the identity function (x => x
in Visual C# or Function(x) x
in Visual Basic) for keySelector
.
ThenBy und ThenByDescending werden definiert, um den-Typ zu erweitern IOrderedEnumerable<TElement> , der auch der Rückgabetyp dieser Methoden ist.ThenBy and ThenByDescending are defined to extend the type IOrderedEnumerable<TElement>, which is also the return type of these methods. Mit diesem Entwurf können Sie mehrere Sortierkriterien angeben, indem Sie eine beliebige Anzahl von- ThenBy oder- ThenByDescending Methoden anwenden.This design enables you to specify multiple sort criteria by applying any number of ThenBy or ThenByDescending methods.
Hinweis
Da IOrderedEnumerable<TElement> von erbt IEnumerable<T> , können Sie OrderBy oder OrderByDescending für die Ergebnisse eines Aufrufes OrderBy , oder Abrufen OrderByDescending ThenBy ThenByDescending .Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Dadurch wird eine neue primäre Reihenfolge eingeführt, die die zuvor festgelegte Reihenfolge ignoriert.Doing this introduces a new primary ordering that ignores the previously established ordering.
Wenn comparer
null
den Wert hat, wird der Standardcomparer Default verwendet, um Schlüssel zu vergleichen.If comparer
is null
, the default comparer Default is used to compare keys.
Diese Methode führt eine stabile Sortierung aus. Das heißt, wenn die Schlüssel zweier Elemente gleich sind, wird die Reihenfolge der Elemente beibehalten.This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. Im Gegensatz dazu behält eine instabile Sortierung nicht die Reihenfolge von Elementen mit demselben Schlüssel bei.In contrast, an unstable sort does not preserve the order of elements that have the same key.