Lorraine Bernard Lorraine Bernard - 1 month ago 8x
Javascript Question

Check if a list is sorted in javascript

In javascript (underscore) , how do I test whether a list of numbers is already sorted or not?


You can use _.every to check whether all elements are in order:

_.every(arr, function(value, index, array) {
  // either it is the first element, or otherwise this element should 
  // not be smaller than the previous element.
  // spec requires string conversion
  return index === 0 || String(array[index - 1]) <= String(value);