API reference@evolu/commonArray › sortArray

Call Signature

function sortArray<T>(
  array: readonly [T, T],
  compareFn?: (a: T, b: T) => number,
): readonly [T, T];

Defined in: packages/common/src/Array.ts:728

Returns a new sorted readonly array.

Wraps native toSorted. Preserves non-empty type.

Sorting without mutation

import { sortArray, type NonEmptyReadonlyArray } from "@evolu/common";

const values: NonEmptyReadonlyArray<number> = [3, 1, 2];
const sorted = sortArray(values, (a, b) => a - b);
expect(sorted).toEqual([1, 2, 3]);
expectTypeOf(sorted).toEqualTypeOf<NonEmptyReadonlyArray<number>>();

Call Signature

function sortArray<T>(
  array: readonly T[],
  compareFn?: (a: T, b: T) => number,
): readonly T[];

Defined in: packages/common/src/Array.ts:733

Possibly empty array.