es-stl - v3.0.2
    Preparing search index...
    • Inserts one value into a sorted array while preserving sort order.

      Type Parameters

      • T

        Element type stored in the array.

      Parameters

      • array: T[]

        Sorted array to mutate.

      • value: T

        Value to insert.

      • callback: SortCallback<T>

        Sort comparator using the same ordering as array.

      Returns T[]

      The same array instance after insertion.

      This function mutates and returns the input array. Equal items remain before the inserted value because insertion occurs at the first item greater than value. The comparator must use the same ascending ordering that was used to sort array.

      const values = [1, 3, 5];
      mergeIntoSortedArray(values, 4, (a, b) => a - b);
      // values === [1, 3, 4, 5]
      const rankings = [
      { name: 'Ada', score: 10 },
      { name: 'Linus', score: 30 },
      ];

      mergeIntoSortedArray(
      rankings,
      { name: 'Grace', score: 20 },
      (a, b) => a.score - b.score
      );
      // rankings.map((item) => item.name) === ['Ada', 'Grace', 'Linus']