@metreeca/flow - v0.10.0
    Preparing search index...

    Function sort

    • Creates a task reordering the feed.

      Items are emitted in the order the comparator establishes; equal items keep their relative source order.

      Warning

      • Exhaustive: the whole feed is drained before the first item is emitted, so an infinite feed never completes.
      • Materialising: the whole feed is held in memory before sorting, so a large feed may exhaust it.
      • Stateful: the ordering covers the items drawn, so a task invoked per nested feed or per run orders each independently rather than the feed as a whole.
      Tip

      The order module of @metreeca/core provides helper functions for assembling complex sorting criteria.

      Type Parameters

      • V

        The type of items in the feed

      Parameters

      • comparator: (a: NoInfer<V>, b: NoInfer<V>) => number = ascending

        The function establishing the relative order of two items, defaulting to ascending, which ranks values in natural order, placing null first

      Returns Task<V>

      A task yielding the items of the feed in comparator order

      await pipe(
      (items([3, 1, 2]))
      (sort())
      (toArray())
      ); // [1, 2, 3]

      await pipe(
      (items([3, 1, 2]))
      (sort(descending))
      (toArray())
      ); // [3, 2, 1]

      await pipe(
      (items([{ age: 30 }, { age: 20 }]))
      (sort(by(x => x.age)))
      (toArray())
      ); // [{ age: 20 }, { age: 30 }]

      await pipe(
      (items(["Émile", "Alice"]))
      (sort((a, b) => a.localeCompare(b)))
      (toArray())
      ); // ["Alice", "Émile"]