# sort-ids

> Calculate sorting identifiers

Latest version **3.0.1** (published 2023-08-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install sort-ids
pnpm add sort-ids
yarn add sort-ids
bun add sort-ids
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2023-08-05 |
| First published | 2018-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Dmitry Yv. |
| Maintainers | dy |
| Keywords | uint8, float, sort, linked-sort, multisort, multiple sort, point-cluster, snap-points, packed sort, sort-numbers, sort pixels, sort colors |

## Links

- npm: https://www.npmjs.com/package/sort-ids
- Repository: https://github.com/dy/sort-ids
- Homepage: https://github.com/dy/sort-ids#readme
- Issues: https://github.com/dy/sort-ids/issues
- npm.io page: https://npm.io/package/sort-ids

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2023-08-05
- 3.0.0 — 2023-08-03
- 2.1.0 — 2018-11-18
- 2.0.1 — 2018-11-18
- 2.0.0 — 2018-11-18
- 1.0.1 — 2018-11-18
- 1.0.0 — 2018-11-18
- 1.4.1 — 2018-11-18

## README

# sort-ids [![test](https://github.com/dy/sort-ids/actions/workflows/test.yml/badge.svg)](https://github.com/dy/sort-ids/actions/workflows/test.yml)

Sort input array, return sorted ids of the array items, keeping the initial array unchanged.

Useful to perform linked sorting of multiple arrays, where linked array[s] should be sorted the same way as the primary one.

[![npm install sort-ids](https://nodei.co/npm/sort-ids.png?mini=true)](https://npmjs.org/package/sort-ids/)

```js
import sortIds from 'sort-ids'
import reorder from 'array-rearrange'

const rates = [.12, .47, .52, .97, ...sourceNumbers]
const names = ['John', 'Alexa', 'Jimmy', 'Kate', ...linkedItems]

const ids = sortIds(rates)

const sortedRates = reorder(rates, ids)
const sortedNames = reorder(names, ids)
```

## `ids = sortIds(array, ids?, precise=true)`

Calculate ids corresponding to sorted input array. The input array isn't changed. Optionally pass input `ids` - some initial order of ids. `precise` flag makes sure no missorts took place and resolves them, if any. Disabling that can save `~30ms` for `1e6` items input arrays.

See also [array-rearrange](https://ghub.io/array-rearrange) for reordering input array based on a list of ids.

## Motivation

This package is >= 6 times faster compared to sorting function. That is achieved by packing input `value - id` pairs into a single `float64` value and performing native sort on that _Float64Array_, then unpacking the `ids` back.


## Acknowledgement

The idea was proposed by [Robert Monfera](https://github.com/monfera) for [snap-points-2d](https://ghub.io/snap-points-2d) and eventually implemented. But there may be other applications, like [sorting colors](https://twitter.com/winkerVSbecks/status/1063919602038685697) etc.

## License

(c) 2018 Dmitry Iv. MIT License

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