使用underscore.js检查列表是否在javascript中排序
问题描述:
在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);
});