Check if a list is sorted in javascript using underscore.js

javascript, underscore.js

Solution

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);
});

Problem

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

Original source