三路比较功能对JavaScript数组 [英] Three-way compare function for Arrays in Javascript
问题描述
有过了解两个数组在Javascript 比较其他问题。我想知道的是写/使用三路比较功能像需要的Array.sort一个()的最直接方法。下面是使用默认的不工作的范例:
> [4,5,10],[4,5,6],[4,1,2]的.sort()//没有比较功能,使用默认的
[[4,1,2],
[4,5,10],//哎呀,字符串排序使得10 LT; 6
[4,5,6]
这是我想出了:
//返回-1,如果LHS是低于RHS,如果+1大,而0,如果相等
//如果LHS和rhs具有不同的长度,只有较短的部分将被认为是
功能compareArrays(左,右){
为(VAR二= 0; II蛋白酶lhs.length;ⅱ++){
如果(左[II]< RHS [II]){
返回-1;
}否则如果(左[II]≥右轴[II]){
返回1;
}
}
返回0;
}
这给了我们我们想要的:
> [4,5,10],[4,5,6],[4,1,2]的.sort(compareArrays)
[[4,1,2],
[4,5,6]中
[4,5,10]]
有什么更像一个班轮,或必须定义自己的职能时,我想这样做?
支持旧的浏览器是不作要求。使用库像jQuery或下划线确定。
看看这一种方法是从一个标准的三路的第一个非零值进行比较适用于每一对元素。但即使如此,我还没有找到一个适合于现有的库。
我会去用功能的方式来使用一个通用的比较,制造商功能:
函数compareArrays(compareItems){
返回功能(A,B){
对于(VAR R,I = 0,1 = Math.min(则为a.length,b.length个); I<升;我++)
如果(0!=(R = compareItems(一个由[i],B [i]于)))
返回ř;
返回则为a.length - b.length个;
};
}
// 例子:
变种compareNumberArrays = compareArray(功能(A,B){返回A-B;}),
compareGenericArrays = compareArray(功能(A,B){返回+(A> B)|| - (B>一个);});
现在你可以使用
[4,5,10],[4,5,6],[4,1,2],[4,5]的.sort(compareNumberArrays)
有什么更像一个班轮,或必须定义自己的职能时,我想这样做?
块引用>比较阵列是一个班轮太复杂了,你应该使用一个辅助的功能。有没有内置其中,你可以使用随处可见。
There have been other questions about Comparing two arrays in Javascript. What I want to know is the most straightforward way to write/use a three-way comparison function like the one required by Array.sort(). Here's an example using the default one which doesn't work well:
> [ [4,5,10], [4,5,6], [4,1,2] ].sort() // no compare function, uses the default one [ [ 4, 1, 2 ], [ 4, 5, 10 ], // oops, string sorting makes 10 < 6 [ 4, 5, 6 ] ]
This is what I came up with:
// return -1 if lhs is "less" than rhs, +1 if "greater", and 0 if equal // if lhs and rhs have different lengths, only the shorter part will be considered function compareArrays(lhs, rhs) { for (var ii = 0; ii < lhs.length; ii++) { if (lhs[ii] < rhs[ii]) { return -1; } else if (lhs[ii] > rhs[ii]) { return 1; } } return 0; }
Which gives us what we want:
> [ [4,5,10], [4,5,6], [4,1,2] ].sort(compareArrays) [ [ 4, 1, 2 ], [ 4, 5, 6 ], [ 4, 5, 10 ] ]
Is there something more like a one-liner, or must I define my own function whenever I want to do this?
Supporting old browsers is not a requirement. Using libraries like jQuery or Underscore is OK.
One way to look at this is "The first nonzero value from a standard three-way compare applied to each pair of elements." But even then I haven't found a good fit in existing libraries.
解决方案I'd go with a generic comparison-maker function to be used in a functional way:
function compareArrays(compareItems) { return function(a, b) { for (var r, i=0, l=Math.min(a.length, b.length); i<l; i++) if (0 != (r = compareItems(a[i], b[i]))) return r; return a.length - b.length; }; } // Examples: var compareNumberArrays = compareArray(function(a,b){ return a-b; }), compareGenericArrays = compareArray(function(a,b){ return +(a>b)||-(b>a); });
Now you can use
[ [4,5,10], [4,5,6], [4,1,2], [4,5] ].sort(compareNumberArrays)
Is there something more like a one-liner, or must I define my own function whenever I want to do this?
Comparing arrays is too complex for a one-liner, you should use a helper function. There's no built-in one that you can use everywhere.
这篇关于三路比较功能对JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!