Enumerable.ThenByDescending メソッド

定義

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

オーバーロード

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

ソース:
OrderBy.cs
ソース:
OrderBy.cs
ソース:
OrderBy.cs

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

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 または 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> これにより、以前に確立された順序を無視する新しいプライマリ順序が導入されます。

この並べ替え方法では、既定の比較子 Defaultを使用してキーを比較します。

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

C# クエリ式の構文では、 句は orderby [first criterion], [second criterion] descendingThenByDescending呼び出しに変換されます。

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

こちらもご覧ください

適用対象

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

ソース:
OrderBy.cs
ソース:
OrderBy.cs
ソース:
OrderBy.cs

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

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)

型パラメーター

TSource

source の要素の型。

TKey

keySelector によって返されるキーの型。

パラメーター

source
IOrderedEnumerable<TSource>

並べ替える要素を格納している IOrderedEnumerable<TElement>

keySelector
Func<TSource,TKey>

各要素からキーを抽出する関数。

comparer
IComparer<TKey>

キーを比較する IComparer<T>

戻り値

要素がキーに従って降順に並べ替えられている IOrderedEnumerable<TElement>

例外

source または keySelectornull です。

次のコード例では、 を使用して、カスタム 比較子を使用 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# または 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 つの要素のキーが等しい場合、要素の順序は保持されます。 一方、不安定な並べ替えでは、同じキーを持つ要素の順序は保持されません。

適用対象