ThenBy<TKey> method

Enumerable<T> ThenBy <TKey>(Selector<T, TKey> keySelector, { EqualityComparer<TKey> keyComparer })

Adds a secondary sorting pass to enumeration in ascending (least-to-greatest) order.

ThenBy applies to an enumerable that has been sorted by OrderBy or OrderByDescending (or another ThenBy or ThenByDescending). Once the previous sorting mechanism is processed, the keys are then sorted again using the EqualityComparer given to this method. (The process of sorting is identical to GroupBy.)

When the type of the enumerable is one of the below types, the EqualityComparer can be omitted. In this case, the function defaults to predefined minimum functions depending on the type:

  • Numeric types (num, int, double) will be sorted by their values in ascending order.
  • String types will be sorted in alphabetic order.

If the enumerable type is not one of these types and the EqualityComparer is not provided, the order of the resulting enumerable is unpredictable.

If the enumerable is already sorted in ascending order, the resulting enumerable will be unchanged.

Implementation

Enumerable<T> ThenBy<TKey>(Selector<T, TKey> keySelector,
    {EqualityComparer<TKey> keyComparer}) {
  if (!(this is InternalOrderedEnumerable))
    throw OperationError(
        'ThenBy must be called immediately following a call to OrderBy, OrderByDescending, ThenBy, or ThenByDescending.');
  return (this as dynamic)
      .createOrderedEnumerable<TKey>(keySelector, keyComparer, false);
}