# @fgiova/sorted-array

> permanent sorted array

Latest version **1.0.0** (published 2023-09-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @fgiova/sorted-array
pnpm add @fgiova/sorted-array
yarn add @fgiova/sorted-array
bun add @fgiova/sorted-array
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-09-15 |
| First published | 2023-09-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Francesco Giovannini |
| Maintainers | fgiova_user |
| Keywords | array, sort, binary search |

## Links

- npm: https://www.npmjs.com/package/@fgiova/sorted-array
- Repository: https://github.com/fgiova/sorted-array
- Homepage: https://github.com/fgiova/sorted-array#readme
- Issues: https://github.com/fgiova/sorted-array/issues
- npm.io page: https://npm.io/package/@fgiova/sorted-array

## 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

- 1.0.0 (latest) — 2023-09-15

## README

# SortedArray
[![NPM version](https://img.shields.io/npm/v/@fgiova/sorted-array.svg?style=flat)](https://www.npmjs.com/package/@fgiova/sorted-array)
![CI workflow](https://github.com/fgiova/sorted-array/actions/workflows/node.js.yml/badge.svg)
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)

## Description
This simple module provides an array implementation, with a sorting feature using binary search algorithm.

Each time an element is inserted, it is placed in the correct position, so that the array is always sorted.<br/>
The sorting is implemented using binary search algorithm, so the complexity is O(log n).<br/>
All the elements are compared using the comparator function assigned on constructor or by default sort function.<br/>
The array as implemented using a native array, so it is not a linked list.<br/>
The array is mutable, but you cannot change the length of the array or remove elements except from the starting or ending array.<br/>
Methods that change the sort order are not implemented; the array is always sorted according to the default order of the comparison function.<br/>

## Installation
```bash
npm install @fgiova/sorted-array
```
## Usage
```typescript
import { SortedArray } from "@fgiova/sorted-array";

const array = new SortedArray<number>();

array.push(1, 3);
array.insert(2);

console.log(array); // [1, 2, 3]
```

### Constructor
```typescript
new SortedArray<T>(items?: T[] comparator?: (a: T, b: T) => number);
```
default comparator is:
```typescript
function comparatorFunction(a, b) {
    if (a < b) return -1;
    if (a >= b) return 1;
    return 0;
}
```
	
### Not implemented methods
- `splice`
- `sort`
- `reverse`
- `copyWithin`
- `fill`

### Benchmark
I have made a simple benchmark using [Benchmark.js](https://benchmarkjs.com/), comparing the performance of the `push` + `sort` methods of the native array and the `insert` method of this module.<br/>
The benchmark is available in the `benchmark` folder.<br/>
The results are the following:

| Function          |                |           |
|-------------------|----------------|-----------|
| Simple Sort Array | 228 ops/sec    | ±91.75%   |
| Sorted Array      | 3,340 ops/sec  | ±46.99%   |

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