javascript sort and sort equals on result. how?
This question already has an answer here:
为了让你排序稳定,你需要比较“相等”项目的索引:
// mass = [{name:…, count:…}, {name:…, count:…}, …]
for (var i=0; i<mass.length; i++)
mass[i].index = i;
mass.sort(function(a, b) {
return compareElements(a, b) || a.index - b.index;
});
function compareElements(a, b) {
// something
return a.count - b.count;
}
If your objects are like:
var arrObj = {
name: "aaa",
count: 1
};
Use this function for comparing:
function compareElements(a, b)
{
return a.count - b.count;
}
链接地址: http://www.djcxy.com/p/19330.html
上一篇: 通过字符串属性排序对象的数组