Lorraine Bernard Lorraine Bernard - 1 year ago 100
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?

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download