英語で読む

次の方法で共有


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

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

C#
public static System.Linq.IOrderedEnumerable<TSource> ThenByDescending<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector);

型パラメーター

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呼び出しに変換されます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

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

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

C#
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);
C#
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);

型パラメーター

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>) して、シーケンス内の要素のセカンダリ順序を降順で実行する方法を示します。

C#
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
*/

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出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 つの要素のキーが等しい場合、要素の順序は保持されます。 これに対し、不安定な並べ替えでは、同じキーを持つ要素の順序は保持されません。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0