@vgerbot/async
    Preparing search index...

    Function sortBy

    • Sorts a collection by computing a sort key for each element using an async function. Elements are sorted in ascending order based on their computed keys.

      Type Parameters

      • I

        The type of the input items.

      • K extends string | number

        The type of the sort key (must be comparable).

      Parameters

      • data: I[] | Promise<I[]>

        The collection to sort.

      • iteratee: (item: I, token: CancellableToken) => Promise<K>

        An async function that computes the sort key for each element.

      • Optionaloptions: SortByOptions<I>

        Configuration options, including concurrency limit.

      Returns CancellableHandle<I[]>

      A cancellable handle that resolves to the sorted array.

      const users = [
      { name: 'Alice', age: 30 },
      { name: 'Bob', age: 25 },
      { name: 'Charlie', age: 35 }
      ];

      const handle = sortBy(
      users,
      async (user, token) => {
      await token.sleep(10);
      return user.age;
      },
      );

      const result = await handle;
      // [{ name: 'Bob', age: 25 }, { name: 'Alice', age: 30 }, { name: 'Charlie', age: 35 }]
      const handle = sortBy(
      { a: 3, b: 1, c: 2 },
      async (value) => value,
      );

      const result = await handle; // [1, 2, 3]
    • Sorts a collection by computing a sort key for each element using an async function. Elements are sorted in ascending order based on their computed keys.

      Type Parameters

      • I

        The type of the input items.

      • K extends string | number

        The type of the sort key (must be comparable).

      Parameters

      • data: CollectionInput<I> | Promise<CollectionInput<I>>

        The collection to sort.

      • iteratee: (item: I, key: string | number, token: CancellableToken) => Promise<K>

        An async function that computes the sort key for each element.

      • Optionaloptions: SortByOptions<I>

        Configuration options, including concurrency limit.

      Returns CancellableHandle<I[]>

      A cancellable handle that resolves to the sorted array.

      const users = [
      { name: 'Alice', age: 30 },
      { name: 'Bob', age: 25 },
      { name: 'Charlie', age: 35 }
      ];

      const handle = sortBy(
      users,
      async (user, token) => {
      await token.sleep(10);
      return user.age;
      },
      );

      const result = await handle;
      // [{ name: 'Bob', age: 25 }, { name: 'Alice', age: 30 }, { name: 'Charlie', age: 35 }]
      const handle = sortBy(
      { a: 3, b: 1, c: 2 },
      async (value) => value,
      );

      const result = await handle; // [1, 2, 3]