Class: SortedArray

SortedArray(comparatoropt, itemsopt)

A Queue that keeps its items sorted using a comparator.

  • Uniqueness is based on the comparator result (0).
  • Insertion maintains order.
  • Use ORDER for common comparators.

Example:


const queue = new SortedArray(ORDER.ASCENDING);
queue.add(3);
queue.add(1);  // inserted before 3

Constructor

new SortedArray(comparatoropt, itemsopt)

Parameters:
Name Type Attributes Default Description
comparator function <optional>
ORDER.ASCENDING

Comparator function (a, b) => -1|0|1

items Array.<*> <optional>
[]

Optional array of items (will be sorted)

Source:

Classes

SortedArray

Members

comparator

Source:

items

Source:

Methods

add(item) → {boolean}

Adds the item in sorted position if no equivalent exists.

Parameters:
Name Type Description
item *
Source:
Returns:

True if added

Type
boolean

has(item) → {boolean}

Tests if an equivalent item exists (based on comparator).

Parameters:
Name Type Description
item *
Source:
Returns:
Type
boolean

remove(item) → {boolean}

Removes an equivalent item (if present).

Parameters:
Name Type Description
item *
Source:
Returns:

True if removed

Type
boolean

(static) logSearch(item, array, comparator, startopt, endopt) → {Array.<number, (*|undefined)>}

Performs binary search over a sorted array.

Parameters:
Name Type Attributes Default Description
item *

The item to search

array Array.<*>

A sorted array

comparator function

Comparison function (a, b) => -1|0|1

start number <optional>
0

Inclusive start index

end number <optional>
array.length

Exclusive end index

Source:
Returns:

A pair [index, result]

Type
Array.<number, (*|undefined)>