使用underscore.js检查列表是否在javascript中排序

Lor*_*ard 9 javascript underscore.js

在javascript(下划线)中,如何测试数字列表是否已经排序?

pim*_*vdb 17

您可以_.every用来检查所有元素是否按顺序排列:

_.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);
});
Run Code Online (Sandbox Code Playgroud)