Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]: Function for checking if array is sorted should check whether the array is sorted in both ascending and descending order. #275

Open
codeme254 opened this issue Nov 15, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@codeme254
Copy link

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

@codeme254 codeme254 added the enhancement New feature or request label Nov 15, 2024
@appgurueu
Copy link
Contributor

appgurueu commented Nov 16, 2024

I don't think that's a good idea. Rather, the function should just take an optional comparator, e.g. like Array.sort.

@codeme254
Copy link
Author

So, your idea is that the function should now be called with two parameters, the array to be sorted and and a callback function which will be the comparator?

isSortedArray([3, 2, 1], (a, b) => b - a); // true
isSortedArray([1, 2, 3], (a, b) => b - a); // false

@appgurueu
Copy link
Contributor

So, your idea is that the function should now be called with two parameters, the array to be sorted and and a callback function which will be the comparator?

isSortedArray([3, 2, 1], (a, b) => b - a); // true
isSortedArray([1, 2, 3], (a, b) => b - a); // false

Yes (your example should have a different comparator in the second line btw). But the comparator should default to something sensible (such that it checks whether the array is sorted ascendingly by default, just like Array.sort sorts ascendingly by default).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants