API Reference > bentleyjs-core > Collections > SortedArray SortedArray<T> Class Maintains an array of some type T in sorted order. The ordering is specified by a function supplied by the user. By default, only unique elements are permitted; attempting to insert a new element that compares as equal to an element already in the array will not modify the contents of the array. This allows a SortedArray to behave like a Set where T is an object and equality is determined by some criterion other than object identity. Because the array is always sorted, querying for the presence of an element is performed using binary search, which is more efficient than a linear search for reasonably large arrays. The user can also specify how the SortedArray takes ownership of inserted values, e.g., by cloning them. The comparison function must meet the following criteria, given 'lhs' and 'rhs' of type T: If lhs is equal to rhs, returns 0 If lhs is less than rhs, returns a negative value If lhs is greater than rhs, returns a positive value If compare(lhs, rhs) returns 0, then compare(rhs, lhs) must also return 0 If compare(lhs, rhs) returns a negative value, then compare(rhs, lhs) must return a positive value, and vice versa. Modifying an element in a way that affects the comparison function will produce unpredictable results, the most likely of which is that the array will cease to be sorted. Implements Iterable<T> Methods Name Description constructor(compare: OrderedComparator<T>, allowDuplicates: boolean = false, clone: CloneFunction<T> = shallowClone): SortedArray Construct a new SortedArray. [Symbol.iterator](): Iterator<T> Returns an iterator over the contents of the array in sorted order, suitable for use in for-of loops. clear(): void Clears the contents of the sorted array. contains(value: T): boolean Returns true if this array contains at least one value comparing equal to the specified value. extractArray(): T[] Extracts the sorted array as a T[] and empties the contents of this SortedArray. findEqual(value: T): T | undefined Looks up an element comparing equal to the specified value using binary search. forEach(func: (value: T) => void): void Apply a function to each element in the array, in sorted order. get(index: number): T | undefined Looks up an element by its index in the array. indexOf(value: T): number Looks up the index of an element comparing equal to the specified value using binary search. insert(value: T, onInsert?: (value: T) => any): number Attempts to insert a new value into the array at a position determined by the ordering. lowerBound(value: T): { equal: boolean, index: number } Protected Computes the position at which the specified value should be inserted to maintain sorted order. remove(value: T): number Removes the first occurrence of a value comparing equal to the specified value from the array. Properties Name Type Description _allowDuplicates Protected boolean _array Protected T[] _clone Protected CloneFunction<T> _compare Protected OrderedComparator<T> isEmpty Accessor ReadOnly boolean Returns true if the array contains no elements. length Accessor ReadOnly number The number of elements in the array Defined in core/bentley/src/SortedArray.ts Line 77 Last Updated: 13 June, 2024