sorted-array

@afoot/sorted-array / Exports / SortedArray

Class: SortedArray

Maintains a sorted array of data.

Example

Instantiated with:

import {SortedArray} from SortedArray;
const arr = new SortedArray();

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new SortedArray(params?)

Define initial data, sort algo, and perform initial sort.

Parameters

Name Type Description
params SortedArrayParams An intitial data array or a config object.

Defined in

index.ts:87

Properties

#config

Private #config: SortedArrayParams

Default settings.

Defined in

index.ts:52


#data

Private #data: DataSet

Instance state. Source of truth for the module.

Defined in

index.ts:48


#sortCallback

Private #sortCallback: SortCallback

Overrides default sorting algorithm.

Defined in

index.ts:50

Accessors

log

get log(): DataSet

Getter to return the sorted data.

Example

const fruit = new SortedArray(['banana', 'pineapple', 'apple']);
fruit.log; //returns ['apple','banana','pineapple']

Returns

DataSet

The sorted instance data.

Defined in

index.ts:182

Methods

#fnSortDefault

Private #fnSortDefault(a, b): number

Built-in sorting algorithm.

Parameters

Name Type Description
a DataItem Comparator to sort against.
b DataItem Comparator to sort against.

Returns

number

One of [0.1,-1] to determine sort order.

Defined in

index.ts:65


#sort

Private #sort(): DataSet

Applies SortCallback on the instance data.

Returns

DataSet

Sorted Data DataSet

Defined in

index.ts:78


add

add(item): DataSet

Add a new element to SortedArray.#data.

Example

const sortedArray = new SortedArray([5, 2]);
sortedArray.add(3); // returns [2,3,5]

Parameters

Name Type Description
item DataItem Input to append to the instance data.

Returns

DataSet

Sorted Array DataSet

Defined in

index.ts:118


delete

delete(index): DataSet

Delete an element from the instance data.

Remarks

This is a departure from the return value of Array.prototype.splice(), which returns an array of the deleted elements. Here, we keep to the pattern and return the new array.

Example

const fruit = new SortedArray(['banana', 'pineapple', 'cheese']);
fruit.delete(1); //returns ['banana','pineapple'];

Parameters

Name Type Description
index number Sorted index of the array to delete.

Returns

DataSet

Sorted Array DataSet

Defined in

index.ts:164


edit

edit(index, item): DataSet

Modify an existing element in SortedArray.#data.

Example

const fruit = new SortedArray(['apples','pizza','bananas']);
const output = fruit.edit(2, 'peaches'); // Index is the _sorted_ array index.
console.log(output); // returns ['apples','bananas','peaches']

Parameters

Name Type Description
index number Sorted index of the array to update.
item DataItem The data to replace the instance entry at that index.

Returns

DataSet

Sorted array of DataItem

Defined in

index.ts:142