Enumerable.ThenByDescending メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
シーケンス内の要素の後続の順序を降順で実行します。
オーバーロード
| 名前 | 説明 |
|---|---|
| ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>) |
キーに従って、シーケンス内の要素の後続の順序を降順で実行します。 |
| ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) |
指定した比較子を使用して、シーケンス内の要素の後続の順序を降順で実行します。 |
ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)
キーに従って、シーケンス内の要素の後続の順序を降順で実行します。
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)
型パラメーター
- TSource
sourceの要素の型。
- TKey
keySelectorによって返されるキーの型。
パラメーター
- source
- IOrderedEnumerable<TSource>
並べ替える要素を含む IOrderedEnumerable<TElement> 。
- keySelector
- Func<TSource,TKey>
各要素からキーを抽出する関数。
返品
キーに従って要素が降順に並べ替えられる IOrderedEnumerable<TElement> 。
例外
source または keySelector が null。
注釈
このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用して列挙されるまで実行されません。
要素自体の値でシーケンスを並べ替える場合は、keySelector の ID 関数 (C# では x => x、Visual Basic では Function(x) x) を指定します。
ThenBy ThenByDescendingは、IOrderedEnumerable<TElement>型を拡張するために定義されています。これは、これらのメソッドの戻り値の型でもあります。 この設計では、任意の数の ThenBy または ThenByDescending メソッドを適用して、複数の並べ替え条件を指定できます。
Note
IOrderedEnumerable<TElement>はIEnumerable<T>から継承されるため、OrderBy、OrderByDescending、ThenBy、またはThenByDescendingの呼び出しの結果に対してOrderByまたはOrderByDescendingを呼び出すことができます。 これにより、以前に確立された順序を無視する新しいプライマリ順序が導入されます。
この並べ替え方法では、既定の比較子 Defaultを使用してキーを比較します。
このメソッドは、安定した並べ替えを実行します。つまり、2 つの要素のキーが等しい場合、要素の順序は保持されます。 これに対し、不安定な並べ替えでは、同じキーを持つ要素の順序は保持されません。
C# クエリ式の構文では、 orderby [first criterion], [second criterion] descending 句は ThenByDescendingの呼び出しに変換されます。
Visual Basicクエリ式構文では、Order By [first criterion], [second criterion] Descending 句が ThenByDescending の呼び出しに変換されます。
こちらもご覧ください
適用対象
ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)
指定した比較子を使用して、シーケンス内の要素の後続の順序を降順で実行します。
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);
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)
型パラメーター
- TSource
sourceの要素の型。
- TKey
keySelectorによって返されるキーの型。
パラメーター
- source
- IOrderedEnumerable<TSource>
並べ替える要素を含む IOrderedEnumerable<TElement> 。
- keySelector
- Func<TSource,TKey>
各要素からキーを抽出する関数。
- comparer
- IComparer<TKey>
キーを比較する IComparer<T> 。
返品
キーに従って要素が降順に並べ替えられる IOrderedEnumerable<TElement> 。
例外
source または keySelector が null。
例
次のコード例では、カスタム比較子を使用して、 ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) を使用して、シーケンス内の要素の 2 次順序を降順で実行する方法を示します。
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
注釈
このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用して列挙されるまで実行されません。
要素自体の値でシーケンスを並べ替える場合は、keySelector の ID 関数 (C# では x => x、Visual Basic では Function(x) x) を指定します。
ThenBy ThenByDescendingは、IOrderedEnumerable<TElement>型を拡張するために定義されています。これは、これらのメソッドの戻り値の型でもあります。 この設計では、任意の数の ThenBy または ThenByDescending メソッドを適用して、複数の並べ替え条件を指定できます。
Note
IOrderedEnumerable<TElement>はIEnumerable<T>から継承されるため、OrderBy、OrderByDescending、ThenBy、またはThenByDescendingの呼び出しの結果に対してOrderByまたはOrderByDescendingを呼び出すことができます。 これにより、以前に確立された順序を無視する新しいプライマリ順序が導入されます。
comparerがnullの場合は、キーの比較に既定の比較子Defaultが使用されます。
このメソッドは、安定した並べ替えを実行します。つまり、2 つの要素のキーが等しい場合、要素の順序は保持されます。 これに対し、不安定な並べ替えでは、同じキーを持つ要素の順序は保持されません。