is-array-sorted
Version:
Check if an Array is sorted
14 lines (11 loc) • 315 B
JavaScript
export default function isArraySorted(array, {comparator} = {}) {
const compare = comparator
? (left, right) => comparator(left, right) > 0
: (left, right) => left > right;
for (const [index, element] of array.entries()) {
if (compare(element, array[index + 1])) {
return false;
}
}
return true;
}