snippetjavascriptTip
Check if a numeric array is sorted in JavaScript
Viewed 0 times
javascriptsortedchecknumericarray
Problem
Sometimes, it's useful to know if an array is sorted in ascending or descending order. This can be especially useful in combination with other algorithms, such as binary search or sorting algorithms.
Sorting the whole array and comparing it to itself is the naive method, yet it's wildly inefficient. The more efficient way is the traditional
Starting with the first two elements, calculate the
> [!NOTE]
>
Sorting the whole array and comparing it to itself is the naive method, yet it's wildly inefficient. The more efficient way is the traditional
for loop and some early returns.Starting with the first two elements, calculate the
direction of the array. If the direction changes at any point, return 0. If the array is empty or has only one element, return 0. If the direction remains the same for the whole array, return the Math.sign() of the direction to get -1 for descending order and 1 for ascending order.> [!NOTE]
>
Solution
const isSorted = arr => {
if (arr.length <= 1) return 0;
const direction = arr[1] - arr[0];
for (let i = 2; i < arr.length; i++) {
if ((arr[i] - arr[i - 1]) * direction < 0) return 0;
}
return Math.sign(direction);
};
isSorted([0, 1, 2, 2]); // 1
isSorted([4, 3, 2]); // -1
isSorted([4, 3, 5]); // 0
isSorted([4]); // 0Starting with the first two elements, calculate the
direction of the array. If the direction changes at any point, return 0. If the array is empty or has only one element, return 0. If the direction remains the same for the whole array, return the Math.sign() of the direction to get -1 for descending order and 1 for ascending order.> [!NOTE]
>
> It's relatively easy to modify the function to work with non-numeric arrays. You can use the
localeCompare method for strings or a custom comparator function for more complex objects.Code Snippets
const isSorted = arr => {
if (arr.length <= 1) return 0;
const direction = arr[1] - arr[0];
for (let i = 2; i < arr.length; i++) {
if ((arr[i] - arr[i - 1]) * direction < 0) return 0;
}
return Math.sign(direction);
};
isSorted([0, 1, 2, 2]); // 1
isSorted([4, 3, 2]); // -1
isSorted([4, 3, 5]); // 0
isSorted([4]); // 0Context
From 30-seconds-of-code: array-is-sorted
Revisions (0)
No revisions yet.