Options
All
  • Public
  • Public/Protected
  • All
Menu

Allows fluently building a comparator which handles multiple fields in an object.

example
const comparator = new ComparisonChain<Person>()
.compareField(p => p.lastName)
.compareField(p => p.firstName)
.buildComparator();
data.sort(comparator);

Type parameters

  • T

Hierarchy

  • ComparisonChain

Index

Constructors

  • new ComparisonChain<T>(fieldGetters?: ((o: T) => any)[], collator?: Collator): ComparisonChain<T>
  • Type parameters

    • T

    Parameters

    • fieldGetters: ((o: T) => any)[] = []

      Optionally takes an array of getter functions for fields to compare. In most cases, it is clearer to give them individually and explicitly with the compareField method.

    • collator: Collator = COLLATOR_DA

      Optionally takes a Collator. This is equivalent to giving it with the withCollator method. By defaults uses a new Intl.Collator('da').

    Returns ComparisonChain<T>

Methods

  • buildComparator(): (a: undefined | null | T, b: undefined | null | T) => number
  • Creates a comparator based on the given fields and Collator.

    Returns (a: undefined | null | T, b: undefined | null | T) => number

      • (a: undefined | null | T, b: undefined | null | T): number
      • Creates a comparator based on the given fields and Collator.

        Parameters

        • a: undefined | null | T
        • b: undefined | null | T

        Returns number

  • Adds a method to extract a given field which should be included in the comparison.

    Parameters

    • fieldGetter: (o: T) => any
        • (o: T): any
        • Parameters

          • o: T

          Returns any

    Returns ComparisonChain<T>

  • Sets the Collator used for comparing strings.

    Parameters

    • collator: Collator

    Returns ComparisonChain<T>