Class SortedArray<T>

The SortedArray type is a subclass of the base JavaScript Array type whose elements are kept in a sorted order.

Methods like SortedArray.insert are provided as an alternative to the typical array push method for inserting new elements in sorted order.

Some methods, such as SortedArray.indexOf provide optimized implementations of Array methods, which may operate more efficiently upon sorted lists.

Warning: The SortedArray type does not stop you from still calling methods like push, shift, or splice, which may invalidate the assumptions that SortedArray methods make about array contents being correctly sorted. These methods should be used with care, if they are used at all. Invalidating the sort order of a SortedArray will cause many operations to behave in unexpected ways.

Type Parameters

  • T

Hierarchy

  • Array<T>
    • SortedArray

Constructors

Properties

[unscopables]: {
    [unscopables]?: boolean;
    length?: boolean;
    [iterator]?: any;
    at?: any;
    concat?: any;
    copyWithin?: any;
    entries?: any;
    every?: any;
    fill?: any;
    filter?: any;
    find?: any;
    findIndex?: any;
    flat?: any;
    flatMap?: any;
    forEach?: any;
    includes?: any;
    indexOf?: any;
    join?: any;
    keys?: any;
    lastIndexOf?: any;
    map?: any;
    pop?: any;
    push?: any;
    reduce?: any;
    reduceRight?: any;
    reverse?: any;
    shift?: any;
    slice?: any;
    some?: any;
    sort?: any;
    splice?: any;
    toLocaleString?: any;
    toString?: any;
    unshift?: any;
    values?: any;
}

Is an object whose properties have the value 'true' when they will be absent when used in a 'with' statement.

Type declaration

  • Optional Readonly [unscopables]?: boolean

    Is an object whose properties have the value 'true' when they will be absent when used in a 'with' statement.

  • Optional length?: boolean

    Gets or sets the length of the array. This is a number one higher than the highest index in the array.

Comparator function used by the SortedArray.

Equality function used by the SortedArray.

length: number

Gets or sets the length of the array. This is a number one higher than the highest index in the array.

reversedCompareFunc: undefined | SortedArrayComparatorFunction<T>

May refer to a reversed version of the SortedArray's comparator function.

[species]: ArrayConstructor

Methods

  • Iterator

    Returns IterableIterator<T>

  • Takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

    Parameters

    • index: number

    Returns undefined | T

  • Get a new array with the values in this array concatenated with any number of other arrays.

    This method is overridden to ensure that a normal Array is returned, instead of a SortedArray. It does not otherwise change the method's behavior.

    Parameters

    • Rest ...items: (T | ConcatArray<T>)[]

      Arrays to concatenate.

    Returns T[]

    a new array containing the concatenated items.

  • Returns the this object after copying a section of the array identified by start and end to the same array starting at position target

    Parameters

    • target: number

      If target is negative, it is treated as length+target where length is the length of the array.

    • start: number

      If start is negative, it is treated as length+start. If end is negative, it is treated as length+end.

    • Optional end: number

      If not specified, length of the this object is used as its default value.

    Returns SortedArray<T>

  • Returns an iterable of key, value pairs for every entry in the array

    Returns IterableIterator<[number, T]>

  • Determines whether all the members of an array satisfy the specified test.

    Type Parameters

    • S

    Parameters

    • predicate: ((value, index, array) => value is S)

      A function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array.

        • (value, index, array): value is S
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns value is S

    • Optional thisArg: any

      An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

    Returns this is S[]

  • Determines whether all the members of an array satisfy the specified test.

    Parameters

    • predicate: ((value, index, array) => unknown)

      A function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array.

        • (value, index, array): unknown
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns unknown

    • Optional thisArg: any

      An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

    Returns boolean

  • Changes all array elements from start to end index to a static value and returns the modified array

    Parameters

    • value: T

      value to fill array section with

    • Optional start: number

      index to start filling the array at. If start is negative, it is treated as length+start where length is the length of the array.

    • Optional end: number

      index to stop filling the array at. If end is negative, it is treated as length+end.

    Returns SortedArray<T>

  • Get a new SortedArray containing only those items which satisfy a given predicate function.

    Type Parameters

    • This = undefined

    Parameters

    Returns SortedArray<T>

    a new SortedArray containing only those values which satisfied the predicate function.

    Params

    predicate A callback function which is invoked for each item in the array. The returned array will contain only those items for which the callback returned a truthy value.

    Params

    thisArg An optional this argument which the predicate function should be called with.

  • Returns the value of the first element in the array where predicate is true, and undefined otherwise.

    Type Parameters

    • S

    Parameters

    • predicate: ((value, index, obj) => value is S)

      find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.

        • (value, index, obj): value is S
        • Parameters

          • value: T
          • index: number
          • obj: T[]

          Returns value is S

    • Optional thisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns undefined | S

  • Parameters

    • predicate: ((value, index, obj) => unknown)
        • (value, index, obj): unknown
        • Parameters

          • value: T
          • index: number
          • obj: T[]

          Returns unknown

    • Optional thisArg: any

    Returns undefined | T

  • Returns the index of the first element in the array where predicate is true, and -1 otherwise.

    Parameters

    • predicate: ((value, index, obj) => unknown)

      find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.

        • (value, index, obj): unknown
        • Parameters

          • value: T
          • index: number
          • obj: T[]

          Returns unknown

    • Optional thisArg: any

      If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    Returns number

  • Returns the first index at which a new value could be inserted into the array and maintain its sort order. This index will be before any other values with an equivalent sort order, if there are any such values in the array.

    Parameters

    • value: T

      The value to check for insertion index.

    • Optional fromIndex: number

      Start comparing items at this index, inclusive. Defaults to 0, i.e. the beginning of the array.

    • Optional endIndex: number

      Stop comparing items at this index, exclusive. Defaults to the length of the array.

    Returns number

    The first valid insertion index for a new value.

  • Get a new array with sub-array items concatenated into it recursively, up to the specified depth.

    This method is overridden to ensure that a normal Array is returned, instead of a SortedArray. It does not otherwise change the method's behavior.

    Type Parameters

    • This

    • Depth extends number = 1

    Parameters

    • this: This
    • Optional depth: Depth

      How deep a nested array structure should be flattened. Defaults to 1.

    Returns FlatArray<This, Depth>[]

    a new array containing the flattened items.

  • Get a new array formed by applying a given transformation callback function to each item in the array, and then flattening the result by one level.

    This method is overridden to ensure that a normal Array is returned, instead of a SortedArray. It does not otherwise change the method's behavior.

    Type Parameters

    • U

    • This = undefined

    Parameters

    • transform: SortedArrayElementCallback<This, T, U | readonly U[]>

      A transformation callback function to be invoked for each item in the array. It should return an array containing new items for the new array, or a single non-array value to be added to the new array.

    • Optional thisArg: This

    Returns U[]

    a new array containing the mapped and flattened items.

  • Performs the specified action for each element in an array.

    Parameters

    • callbackfn: ((value, index, array) => void)

      A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

        • (value, index, array): void
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns void

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

    Returns void

  • Get all equal values in the array and return them as a new SortedArray.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Parameters

    • value: T

      Return values in the array that are equal to this one.

    Returns SortedArray<T>

    the equivalent elements as a new SortedArray.

  • Remove all equal values from the array and return those removed values as a new SortedArray.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Parameters

    • value: T

      Remove the last item in the array that is equal to this input value.

    Returns SortedArray<T>

    the removed elements as a new SortedArray.

  • Check whether any item in the array is equal to the given value.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Parameters

    • value: T

      Check whether there is any value in the array that is equal to this one.

    • Optional fromIndex: number

      Start looking for items at this index, inclusive. Defaults to 0, i.e. the beginning of the array.

    Returns boolean

    true if an equal item was found in the array, or false if not.

  • Get the index of the first item in the array that is equal to the given value, or -1 if there is no equal item in the array.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Parameters

    • value: T

      Find the first index of a value in the array that is equal to this one.

    • Optional fromIndex: number

      Start looking for items at this index, inclusive. Defaults to 0, i.e. the beginning of the array.

    Returns number

    the index of the first equal item, or -1 if no equal item was found.

  • Get the index of the first item in the array that is equal to the given value, or -1 if there is no equal item in the array.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Parameters

    • value: T

      Find the first index of a value in the array that is equal to this one.

    • Optional fromIndex: number

      Start looking for items at this index, inclusive. Defaults to 0, i.e. the beginning of the array.

    • Optional endIndex: number

      Stop looking for items at this index, exclusive. Defaults to the length of the array.

    Returns number

    the index of the first equal item, or -1 if no equal item was found.

  • Insert a value into the array, maintaining sort order.

    You probably want to use this method instead of push.

    Parameters

    • value: T

      The value to insert into the array.

    Returns number

    the new length of the array.

  • Insert an iterable of assumed-sorted values into the array.

    This should typically be more efficient than calling SortedArray.insert in a loop.

    Parameters

    • values: Iterable<T> | ArrayLike<T>

      An iterable of values to be sorted into the array, which are already sorted according to this SortedArray's same comparator.

    Returns number

    The new length of the array.

    Throws

    TypeError if the input was not iterable.

  • Check whether the contents of the array are in fact sorted as expected.

    Returns boolean

    true when the contents of the array are correctly sorted according to its comparator function, or false otherwise.

  • Adds all the elements of an array into a string, separated by the specified separator string.

    Parameters

    • Optional separator: string

      A string used to separate one element of the array from the next in the resulting string. If omitted, the array elements are separated with a comma.

    Returns string

  • Returns an iterable of keys in the array

    Returns IterableIterator<number>

  • Get the index of the last item in the array that is equal to the given value, or -1 if there is no equal item in the array.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Note that the behavior of this method differs from the normal Array lastIndexOf method in that passing undefined explicitly as the endIndex argument is treated the same as omitting that argument, instead of coercing the value to 0.

    Parameters

    • value: T

      Find the last index of a value in the array that is equal to this one.

    • Optional endIndex: number

      Stop looking for items at this index. Defaults to the end of the array.

    Returns number

    the index of the last equal item, or -1 if no equal item was found.

  • Get the index of the last item in the array that is equal to the given value, or -1 if there is no equal item in the array.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Parameters

    • value: T

      Find the last index of a value in the array that is equal to this one.

    • Optional fromIndex: number

      Start looking for items at this index, inclusive. Defaults to 0, i.e. the beginning of the array.

    • Optional endIndex: number

      Stop looking for items at this index, exclusive. Defaults to the length of the array.

    Returns number

    the index of the last equal item, or -1 if no equal item was found.

  • Returns the last index at which a new value could be inserted into the array and maintain its sort order. This index will be after any other values with an equivalent sort order, if there are any such values in the array.

    Parameters

    • value: T

      The value to check for insertion index.

    • Optional fromIndex: number

      Start comparing items at this index, inclusive. Defaults to 0, i.e. the beginning of the array.

    • Optional endIndex: number

      Stop comparing items at this index, exclusive. Defaults to the length of the array.

    Returns number

    The last valid insertion index for a new value.

  • Get a new array populated with the results of calling a transformation callback function on every item in this array.

    This method is overridden to ensure that a normal Array is returned, instead of a SortedArray. It does not otherwise change the method's behavior.

    Type Parameters

    • U

    • This = undefined

    Parameters

    • transform: SortedArrayElementCallback<This, T, U>

      A transformation callback function to be invoked for each item in the array. Its return value is added as a single item in the new array.

    • Optional thisArg: This

    Returns U[]

    a new array containing the values returned by calls to the transformation function.

  • Removes the last element from an array and returns it. If the array is empty, undefined is returned and the array is not modified.

    Returns undefined | T

  • Appends new elements to the end of an array, and returns the new length of the array.

    Parameters

    • Rest ...items: T[]

      New elements to add to the array.

    Returns number

  • Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => T)

      A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

        • (previousValue, currentValue, currentIndex, array): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns T

    Returns T

  • Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => T)
        • (previousValue, currentValue, currentIndex, array): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns T

    • initialValue: T

    Returns T

  • Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    Type Parameters

    • U

    Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => U)

      A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

        • (previousValue, currentValue, currentIndex, array): U
        • Parameters

          • previousValue: U
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns U

    • initialValue: U

      If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

    Returns U

  • Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => T)

      A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

        • (previousValue, currentValue, currentIndex, array): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns T

    Returns T

  • Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => T)
        • (previousValue, currentValue, currentIndex, array): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns T

    • initialValue: T

    Returns T

  • Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    Type Parameters

    • U

    Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => U)

      A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

        • (previousValue, currentValue, currentIndex, array): U
        • Parameters

          • previousValue: U
          • currentValue: T
          • currentIndex: number
          • array: T[]

          Returns U

    • initialValue: U

      If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

    Returns U

  • Remove the first equal value from the array.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Parameters

    • value: T

      Remove the first item in the array that is equal to this input value.

    Returns boolean

    true if a matching element was found and removed, or false if no matching element was found.

  • Remove all equal values from the array.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Parameters

    • value: T

      Remove all items in the array that are equal to this input value.

    Returns number

    the number of removed array items.

  • Remove the last equal value from the array.

    Equality is determined using the SortedArray's equality function, which is SortedArray.DefaultEqualityFunction when not otherwise specified.

    Parameters

    • value: T

      Remove the last item in the array that is equal to this input value.

    Returns boolean

    true if a matching element was found and removed, or false if no matching element was found.

  • Reverse the items in the array, and update the array's comparator function to account for this new reversed sort order. Later insertions into this SortedArray will respect the reversed sort order.

    Returns SortedArray<T>

    this SortedArray.

  • Removes the first element from an array and returns it. If the array is empty, undefined is returned and the array is not modified.

    Returns undefined | T

  • Get the items in the array from a start to an end index as a new SortedArray.

    Parameters

    • Optional start: number

      Get items starting at this index, inclusive. Defaults to 0, i.e. the beginning of the array.

    • Optional end: number

      Get items ending at this index, exclusive. Defaults to the length of the array.

    Returns SortedArray<T>

    a new SortedArray containing the given array slice.

  • Determines whether the specified callback function returns true for any element of an array.

    Parameters

    • predicate: ((value, index, array) => unknown)

      A function that accepts up to three arguments. The some method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value true, or until the end of the array.

        • (value, index, array): unknown
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns unknown

    • Optional thisArg: any

      An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

    Returns boolean

  • Remove items from the array, starting at a given index.

    Parameters

    • start: number

      Start removing items from the array at this index.

    • Optional deleteCount: number

      The number of items that should be removed.

    Returns SortedArray<T>

    A new SortedArray containing the removed items.

  • Remove items from the array, starting at a given index, and insert new items at that index.

    Warning: If inserting the provided items would invalidate the implementation's assumptions about array items being in sorted order, then the implementation will behave in unexpected ways. Use this method with caution.

    Parameters

    • start: number

      Start removing items from the array at this index.

    • deleteCount: number

      The number of items that should be removed.

    • Rest ...items: T[]

      A list of items which should be newly inserted in place of the removed items. The implementation assumes that these items can be inserted without violating the sort order of the SortedArray.

    Returns SortedArray<T>

    A new SortedArray containing the removed items.

  • Returns a string representation of an array. The elements are converted to string using their toLocaleString methods.

    Returns string

  • Returns a string representation of an array.

    Returns string

  • Inserts new elements at the start of an array, and returns the new length of the array.

    Parameters

    • Rest ...items: T[]

      Elements to insert at the start of the array.

    Returns number

  • Returns an iterable of values in the array

    Returns IterableIterator<T>

  • Construct a new SortedArray, initializing it with some values in an iterable or array-like object that are already provided in sorted order.

    Type Parameters

    • T

    Parameters

    Returns SortedArray<T>

    the newly constructed SortedArray, containing the given assumed-sorted values.

  • Parameters

    • arg: any

    Returns arg is any[]

  • Construct a new SortedArray and insert the given values.

    Type Parameters

    • T

    Parameters

    • Rest ...values: T[]

      The values that should be inserted and sorted in the newly constructed SortedArray.

    Returns SortedArray<T>

    the newly constructed SortedArray, containing the given values in sorted order.

  • Construct a new SortedArray, initializing it with some values that are already provided in sorted order.

    Type Parameters

    • T

    Parameters

    • Rest ...values: T[]

      The values that should be inserted in the newly constructed SortedArray. They must be sorted according to SortedArray.DefaultComparatorFunction, otherwise the returned SortedArray may behave unexpectedly.

    Returns SortedArray<T>

    the newly constructed SortedArray, containing the given assumed-sorted values.

Generated using TypeDoc