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

    Function sort

    • Creates a task reordering the feed.

      The whole feed is drained before the first item is emitted, then items are emitted in the order the comparator establishes; equal items keep their relative source order.

      Warning

      Accumulates the whole feed in memory before sorting. For large or infinite feeds, this may exhaust memory or never complete.

      Tip

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

      Type Parameters

      • V

        The type of items in the feed

      Parameters

      • comparator: (a: V, b: 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"]