The reference to the same sorted array.
The last parameter insertAfterEqual
will determine where to insert when
there are equal elements:
true(default)
e.g [equal, equal, toInsert]
false
e.g [toInsert, equal, equal]
Note that setting this to false will be slower since more elements need to
be shifted to the right.sortedInsert(
[1, 2, 4, 5],
3,
(item, toInsert) => item - toInsert
) // [1, 2, 3, 4, 5]
By default an element is inserted after existing equal elements:
// 'insert' element will come AFTER existing 'foo'
sortedInsert(
[{ n: 0, v: 'foo' }, { n: 9, v: 'bar' }],
{ n: 0, v: 'insert' },
(item, toInsert) => item.n - toInsert.n
) // [{ n: 0, v: 'foo' }, { n: 0, v: 'insert' }, { n: 9, v: 'bar' }]
Change this by setting insertAfterEqual
to false
:
// now 'insert' element will come BEFORE existing 'foo'
sortedInsert(
..., // same as above
false
) // [{ n: 0, v: 'insert' }, { n: 0, v: 'foo' }, { n: 9, v: 'bar' }]
Generated using TypeDoc
Insert an element into a sorted array using a binary search. The array must already be sorted and will be mutated in-place.