C# Enumerable ThenBy(IOrderedEnumerable, Func)

Description

Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.

Syntax


public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
  this IOrderedEnumerable<TSource> source,
  Func<TSource, TKey> keySelector
)

Parameters

  • TSource - The type of the elements of source.
  • TKey - The type of the key returned by keySelector.
  • source - An IOrderedEnumerable that contains elements to sort.
  • keySelector - A function to extract a key from each element.

Example

The following code example demonstrates how to use ThenBy to perform a secondary ordering of the elements in a sequence.


/*from w  w w.ja v a  2  s.c  om*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  

      string[] fruits = { "grape",  "apple", "blueberry" };

      IEnumerable<string> query =
          fruits.OrderBy(fruit => fruit.Length).ThenBy(fruit => fruit);

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

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable