Open
Description
Motivation
I have been looking at the code for checking whether an array is sorted and this is the current implementation:
export function isSortedArray(arr: number[]): boolean {
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] >= arr[i + 1]) {
return false
}
}
return true
}
The code is working fine, but currently, it is only checking whether the passed array is sorted in ascending order, should we want to check whether the array is sorted in descending order then it will not be of much help.
Examples
Currently, the function checks whether an array is sorted, taking a single parameter: the array to evaluate. To enhance its functionality, I propose adding a second parameter that specifies the order to check for sorting.
The second parameter should be a string such as:
asc
to check whether the array is sorted in ascending order.desc
to check whether the array is sorted in descending order.
Here are some example:
isSortedArray([1, 2, 3, 5, 9], 'asc') // true
isSortedArray([1, 2, 3, 5, 9], 'desc') // false
isSortedArray([9, 5, 3, 2, 1], 'desc') // true
Possible workarounds
No response