Determine whether an integer array is sorted in JavaScript
This post will discuss how to determine whether an integer array is sorted in JavaScript.
There are several ways to check whether an array is sorted in JavaScript. Here are some of the possible functions, along with some examples:
1. Using Array.every() function
The Array.every() function tests whether all elements in the array pass a test implemented by a provided function. We can use this function to check whether each element in the array is greater than or equal to the previous element, for ascending order, or less than or equal to the previous element, for descending order. Here’s an example:
|
1 2 3 4 5 |
let arr = [1, 2, 3, 4, 5]; let isAscending = arr.every((value, index) => index === 0 || value >= arr[index - 1]); console.log(isAscending); // true |
This function returns true if every element in the array is greater than or equal to the previous element, or if it is the first element. To check if the array is descending, we can simply flip the comparison operator to <=.
|
1 2 3 4 5 |
let arr = [5, 4, 3, 2, 1]; let isDescending = arr.every((value, index) => index === 0 || value <= arr[index - 1]); console.log(isDescending); // true |
2. Using Lodash or Underscore Library
Another way is to use a library like Lodash or Underscore that provides a function to check if an array is sorted. We can use the _.every() function to check if every element is in order as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// import the library const _ = require("lodash"); // or const _ = require("underscore"); // use the _.every function to check if every element is in order function isAscending(arr) { return _.every(arr, function(value, index, array) { // either it is the first element, or otherwise this element should // not be smaller than the previous element. return index === 0 || array[index - 1] <= value; }); } let arr = [1, 2, 3, 4, 5]; console.log(isAscending(arr)); // true |
3. Using a loop
One way is to use a loop to iterate over the array and compares each element with the next element, using a flag variable to indicate whether the array is sorted or not. We can use a different comparison operator for ascending or descending order. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
let arr = [1, 2, 3, 4, 5]; // if the array is empty or has one element, it is sorted let isAscending = true; let isDescending = true; // loop through the array from the first to the second last element for (let i = 0; i < arr.length - 1; i++) { // if the current element is greater than the next one, // the array is not sorted in ascending order if (arr[i] > arr[i + 1]) { isAscending = false; } // if the current element is less than the next one, // the array is not sorted in descending order if (arr[i] < arr[i + 1]) { isDescending = false; } } console.log(isAscending); // true console.log(isDescending); // false |
4. Using a custom function
Another way is to define a custom function that can check whether an array is sorted by comparing the last two elements of the array, and then recursively calling itself on the subarray that excludes the last element. We can use a different comparison operator for ascending or descending order. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// a custom function that can check whether an array is sorted in ascending order function isAscending(arr) { // if the array is empty or has one element, it is sorted if (arr.length <= 1) { return true; } // get the last two elements of the array let last = arr[arr.length - 1]; let secondLast = arr[arr.length - 2]; // if the last element is smaller than the second last one, the array is not sorted if (last < secondLast) { return false; } // otherwise, call the function recursively with a smaller array return isAscending(arr.slice(0, -1)); } let arr = [1, 2, 3, 4, 5]; console.log(isAscending(arr)); // true |
That’s all about determining whether an integer array is sorted in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)