Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/samualtnorman/lib. A collection of common utilities I need. www.npmjs.com/package/@samual/lib
Something went wrong. Try again.
1234567891011121314151617export class SortedList<T> { public readonly array: readonly T[]
constructor(private readonly comparer: (a: T, b: T) => number, items?: Iterable<T>) { this.array = items ? [ ...items ].sort(comparer) : [] }
add(item: T) { const index = this.array.findIndex(arrayItem => this.comparer(arrayItem, item) >= 0)
if (index == -1) (this.array as T[]).push(item) else (this.array as T[]).splice(index, 0, item) }}