import { BasicEnumerable } from './BasicEnumerable'; import { applyThenBy } from '../functions/applicators/applyThenBy'; import { Comparer, IEnumerableFactory, IOrderedEnumerable } from '../types'; /** * Represents a sorted sequence. * @typeparam TSource The type of the elements of the sequence. */ export class OrderedEnumerable extends BasicEnumerable implements IOrderedEnumerable { private readonly orderedPairs: () => Generator; public constructor(factory: IEnumerableFactory, orderedPairs: () => Generator) { super(factory, function* (): Generator { for (const pair of orderedPairs()) { yield* pair; } }); this.orderedPairs = orderedPairs; } /** * Performs a subsequent ordering of the elements in a sequence in ascending order. * @typeparam TKey The type of the key returned by keySelector. * @param keySelector A function to extract a key from each element. * @returns An IOrderedEnumerable whose elements are sorted according to a key. */ public thenBy(keySelector: (item: TSource) => TKey): IOrderedEnumerable; /** * Performs a subsequent ordering of the elements in a sequence in ascending order. * @typeparam TKey The type of the key returned by keySelector. * @param keySelector A function to extract a key from each element. * @param comparer An Comparer to compare keys. * @returns An IOrderedEnumerable whose elements are sorted according to a key. */ public thenBy(keySelector: (item: TSource) => TKey, comparer: Comparer): IOrderedEnumerable; public thenBy(keySelector: (item: TSource) => TKey, comparer?: Comparer): IOrderedEnumerable { return applyThenBy(this.factory, this.orderedPairs, true, keySelector, comparer); } /** * Performs a subsequent ordering of the elements in a sequence in descending order. * @typeparam TKey The type of the key returned by keySelector. * @param keySelector A function to extract a key from each element. * @returns An IOrderedEnumerable whose elements are sorted according to a key. */ public thenByDescending(keySelector: (item: TSource) => TKey): IOrderedEnumerable; /** * Performs a subsequent ordering of the elements in a sequence in descending order. * @typeparam TKey The type of the key returned by keySelector. * @param keySelector A function to extract a key from each element. * @param comparer An Comparer to compare keys. * @returns An IOrderedEnumerable whose elements are sorted according to a key. */ public thenByDescending( keySelector: (item: TSource) => TKey, comparer: Comparer ): IOrderedEnumerable; public thenByDescending( keySelector: (item: TSource) => TKey, comparer?: Comparer ): IOrderedEnumerable { return applyThenBy(this.factory, this.orderedPairs, false, keySelector, comparer); } }