Function sortWith

  • Returns a new array sorted according to the comparator function. The comparator module contains some useful comparator functions and utilities for creating others. You may also write your own comparators, provided that they follow the specifications found in [MDN on Array.prototype.sort()] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).

    Remarks

    The built-in Array.prototype.sort() has a default comparator that sorts in ascending order according to the string representation of the items. However, this behavior is rarely the most sound option for real world use cases, and Flurp has therefore not included such an option among its comparators.

    Example

    import * as A from "flurp/array";
    import * as C from "flurp/comparator";

    const sortScores = A.sortWith(C.numericDesc);
    sortScores([30, 6, 1, 40, 5]); // [40, 30, 6, 5, 1]

    Type Parameters

    • T

    Parameters

    • comparator: ((x: T, y: T) => number)
        • (x: T, y: T): number
        • Parameters

          • x: T
          • y: T

          Returns number

    Returns ((arr: readonly T[]) => T[])

      • (arr: readonly T[]): T[]
      • Parameters

        • arr: readonly T[]

        Returns T[]