# array-push-at-sort-position

> Push items to an array at their correct sort-position

Latest version **5.0.0** (published 2026-03-16) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install array-push-at-sort-position
pnpm add array-push-at-sort-position
yarn add array-push-at-sort-position
bun add array-push-at-sort-position
```

## Health

**Score 70/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2026-03-16 |
| First published | 2019-11-22 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 22.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 9 |
| Author | pubkey |
| Maintainers | pubkey |
| Keywords | array, push, sort, insert, binary-search, sorting, binary-insert, indexing |

## Links

- npm: https://www.npmjs.com/package/array-push-at-sort-position
- Repository: https://github.com/pubkey/array-push-at-sort-position
- Homepage: https://github.com/pubkey/array-push-at-sort-position#readme
- Issues: https://github.com/pubkey/array-push-at-sort-position/issues
- npm.io page: https://npm.io/package/array-push-at-sort-position

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 5.0.0 (latest) — 2026-03-16
- 4.0.1 — 2023-10-09
- 4.0.0 — 2023-10-08
- 3.0.0 — 2023-04-07
- 2.0.0 — 2022-04-15
- 1.2.0 — 2020-01-28
- 1.1.0 — 2020-01-26
- 1.0.3 — 2019-11-25
- 1.0.2 — 2019-11-22
- 1.0.1 — 2019-11-22
- 1.0.0 — 2019-11-22

## README

# array-push-at-sort-position

Push items to an array at their correct sort-position which is much faster then re-sorting the array.

Adding an item to an array with `push()` and `sort()` has `O(n*log(n))`
while inserting the item at the correct sort-position has `O(n)`.

```bash
  npm install array-push-at-sort-position --save
```

```typescript

// instead of pushing and resorting like this:
const arrayWithItems = arrayWithItems.slice();
arrayWithItems.push(newItem);
const arrayWithNewItems = arrayWithItems.sort(sortComparator);

// you can push the newItem directly into the correct sorting position
const insertPosition = pushAtSortPosition(
  arrayWithItems,
  newItem,
  sortComparator,
  /**
   * Start lowest index
   * Use 0 by default. If you use this method to merge sorted arrays, you might
   * use a  higher value if you know that the newItem will not be positioned before that index.
   */
  0
);
```

### Important

- Calling `pushAtSortPosition` will not copy the array. It will mutate the input array. Call `array.slice(0)` on the input
if you do not want the original array to be mutated.

### See also

I tested many implementations and refactored the best ones. Some other modules:

- [binary-search-insert](https://www.npmjs.com/package/binary-search-insert)
- [sorted-array](https://github.com/aaditmshah/sorted-array/blob/master/sorted-array.js#L11)

---
_Source: https://npm.io/package/array-push-at-sort-position · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
