Compartir a través de


IOrderedEnumerable<TElement>.CreateOrderedEnumerable<TKey> Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Performs a subsequent ordering on the elements of an IOrderedEnumerable<TElement> according to a key.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Function CreateOrderedEnumerable(Of TKey) ( _
    keySelector As Func(Of TElement, TKey), _
    comparer As IComparer(Of TKey), _
    descending As Boolean _
) As IOrderedEnumerable(Of TElement)
IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey>(
    Func<TElement, TKey> keySelector,
    IComparer<TKey> comparer,
    bool descending
)

Type Parameters

  • TKey
    The type of the key produced by keySelector.

Parameters

  • descending
    Type: System.Boolean
    true to sort the elements in descending order; false to sort the elements in ascending order.

Return Value

Type: System.Linq.IOrderedEnumerable<TElement>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.

Remarks

The functionality provided by this method is like that provided by ThenBy or ThenByDescending, depending on whether descending is true or false. They both perform a subordinate ordering of an already sorted sequence of type IOrderedEnumerable<TElement>.

Examples

The following code example demonstrates how to use CreateOrderedEnumerable<TKey> to perform a secondary ordering on an IOrderedEnumerable<TElement>.

      ' Create an array of strings to sort.
      Dim fruits() As String = {"apricot", "orange", "banana", "mango", "apple", "grape", "strawberry"}
      ' First sort the strings by their length.
      Dim sortedFruits2 As IOrderedEnumerable(Of String) = _
          fruits.OrderBy(Function(fruit) fruit.Length)
      ' Secondarily sort the strings alphabetically, using the default comparer.
      Dim sortedFruits3 As IOrderedEnumerable(Of String) = _
          sortedFruits2.CreateOrderedEnumerable(Of String)( _
              Function(fruit) fruit, _
              System.Collections.Generic.Comparer(Of String).Default, _
              False)

      Dim output As New System.Text.StringBuilder
      ' Output the resulting sequence of strings.
      For Each fruit As String In sortedFruits3
         output.AppendLine(fruit)
      Next

      ' Display the results.
      outputBlock.Text &= output.ToString() & vbCrLf

      ' This code produces the following output:
      '
      ' apple
      ' grape
      ' mango
      ' banana
      ' orange
      ' apricot
      ' strawberry

      // Create an array of strings to sort.
      string[] fruits = { "apricot", "orange", "banana", "mango", "apple", "grape", "strawberry" };
      // First sort the strings by their length.
      IOrderedEnumerable<string> sortedFruits2 =
          fruits.OrderBy(fruit => fruit.Length);
      // Secondarily sort the strings alphabetically, using the default comparer.
      IOrderedEnumerable<string> sortedFruits3 =
          sortedFruits2.CreateOrderedEnumerable<string>(
              fruit => fruit,
              Comparer<string>.Default, false);

      // Output the resulting sequence of strings.
      foreach (string fruit in sortedFruits3)
         outputBlock.Text += fruit + "\n";

      // This code produces the following output:
      //
      // apple
      // grape
      // mango
      // banana
      // orange
      // apricot
      // strawberry

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.