Enumerable.ThenBy メソッド

定義

シーケンス内の後続の要素を昇順で配置します。

オーバーロード

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)

キーに従って、シーケンス内の後続の要素を昇順で配置します。

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

指定された比較子を使用して、シーケンス内の後続の要素を昇順で配置します。

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)

キーに従って、シーケンス内の後続の要素を昇順で配置します。

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedEnumerable<TSource> ^ ThenBy(System::Linq::IOrderedEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static System.Linq.IOrderedEnumerable<TSource> ThenBy<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member ThenBy : System.Linq.IOrderedEnumerable<'Source> * Func<'Source, 'Key> -> System.Linq.IOrderedEnumerable<'Source>
<Extension()>
Public Function ThenBy(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 または keySelectornull です。

次のコード例では、 を使用 ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>) して、シーケンス内の要素のセカンダリ順序付けを実行する方法を示します。

string[] fruits = { "grape", "passionfruit", "banana", "mango",
                      "orange", "raspberry", "apple", "blueberry" };

// Sort the strings first by their length and then
//alphabetically by passing the identity selector function.
IEnumerable<string> query =
    fruits.OrderBy(fruit => fruit.Length).ThenBy(fruit => fruit);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}

/*
    This code produces the following output:

    apple
    grape
    mango
    banana
    orange
    blueberry
    raspberry
    passionfruit
*/
' Create an array of strings.
Dim fruits() As String =
{"grape", "passionfruit", "banana", "mango",
 "orange", "raspberry", "apple", "blueberry"}

' Sort the strings first by their length and then
' alphabetically by passing the identity function.
Dim query As IEnumerable(Of String) =
fruits _
.OrderBy(Function(fruit) fruit.Length) _
.ThenBy(Function(fruit) fruit)

' 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())

' This code produces the following output:
'
' apple
' grape
' mango
' banana
' orange
' blueberry
' raspberry
' passionfruit

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# For Each または Visual Basic で を使用foreachして列挙されるまで実行されません。

要素自体の値でシーケンスを並べ替える場合は、 の ID 関数 (x => x C# または Function(x) x Visual Basic の場合) keySelectorを指定します。

ThenByThenByDescending は、 型 IOrderedEnumerable<TElement>を拡張するために定義されています。これは、これらのメソッドの戻り値の型でもあります。 この設計では、任意の数の メソッドまたは ThenByDescending メソッドを適用して、複数のThenBy並べ替え条件を指定できます。

注意

は から継承されるためIOrderedEnumerable<TElement>、または OrderByDescending の呼び出OrderByしの結果で または ThenByDescendingOrderByOrderByDescendingThenBy呼び出すことができます。IEnumerable<T> これにより、以前に確立された順序を無視する新しいプライマリ順序が導入されます。

このメソッドは、既定の比較子 Defaultを使用してキーを比較します。

このメソッドは、安定した並べ替えを実行します。つまり、2 つの要素のキーが等しい場合、要素の順序は保持されます。 これに対し、不安定な並べ替えでは、同じキーを持つ要素の順序は保持されません。

クエリ式の構文では、 orderby [first criterion], [second criterion] (C#) 句または Order By [first criterion], [second criterion] (Visual Basic) 句は の ThenBy呼び出しに変換されます。

こちらもご覧ください

適用対象

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

指定された比較子を使用して、シーケンス内の後続の要素を昇順で配置します。

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedEnumerable<TSource> ^ ThenBy(System::Linq::IOrderedEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer);
public static System.Linq.IOrderedEnumerable<TSource> ThenBy<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey> comparer);
public static System.Linq.IOrderedEnumerable<TSource> ThenBy<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);
static member ThenBy : System.Linq.IOrderedEnumerable<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IComparer<'Key> -> System.Linq.IOrderedEnumerable<'Source>
<Extension()>
Public Function ThenBy(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 または keySelectornull です。

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# For Each または Visual Basic で を使用foreachして列挙されるまで実行されません。

要素自体の値でシーケンスを並べ替える場合は、 の ID 関数 (x => x C# または Function(x) x Visual Basic の場合) keySelectorを指定します。

ThenByThenByDescending は、 型 IOrderedEnumerable<TElement>を拡張するために定義されています。これは、これらのメソッドの戻り値の型でもあります。 この設計では、任意の数の メソッドまたは ThenByDescending メソッドを適用して、複数のThenBy並べ替え条件を指定できます。

注意

は から継承されるためIOrderedEnumerable<TElement>、または OrderByDescending の呼び出OrderByしの結果で または ThenByDescendingOrderByOrderByDescendingThenBy呼び出すことができます。IEnumerable<T> これにより、以前に確立された順序を無視する新しいプライマリ順序が導入されます。

nullの場合comparer、キーの比較に既定の比較子Defaultが使用されます。

このメソッドは、安定した並べ替えを実行します。つまり、2 つの要素のキーが等しい場合、要素の順序は保持されます。 これに対し、不安定な並べ替えでは、同じキーを持つ要素の順序は保持されません。

適用対象