我的数组对象如下
var data = [{"weight":0,"name":"New Arrivals"},{"weight":0,"name":"Weekly Promotions"},{"weight":0,"name":"Sale"},{"weight":0,"name":"Extended Size"},{"weight":0,"name":"Accessories and Shoes"},{"weight":0,"name":"Activewear"},{"weight":0,"name":"Disney Project"},{"weight":0,"name":"Dresses and Jumpsuits"},{"weight":0,"name":"Fleece"},{"weight":0,"name":"HEATTECH Collection"},{"weight":0,"name":"Ines de la Fressange"},{"weight":0,"name":"Intimates"},{"weight":0,"name":"Jeans"},{"weight":0,"name":"Loungewear"},{"weight":0,"name":"Outerwear and Blazers"},{"weight":0,"name":"Pants"},{"weight":0,"name":"Shirts and Blouses"},{"weight":0,"name":"Skirts"},{"weight":0,"name":"Socks and Hosiery"},{"weight":0,"name":"Sweaters"},{"weight":0,"name":"Sweatshirts and Sweatpants"},{"weight":0,"name":"T-Shirts and Tops"},{"weight":0,"name":"UT: Graphic Tees"},{"weight":0,"name":"Wear To Work"},{"weight":0,"name":"Mix and Match"}]
我试图根据属性权重对此进行排序,但是如果权重的所有值都是0,我需要原始顺序,因为它在输出上。但是这个功能没有按预期工作,而是订单正在改变。
使用过ramda以及JavaScript排序
var sortedData = R.sortBy(R.prop('weight'), data);
或
var sortData = data.slice(0);
sortData.sort(function(a,b) {
return a.weight - b.weight;
});
这两种情况都得到如下结果
var output = [{"weight":0,"name":"Jeans"},{"weight":0,"name":"New Arrivals"},{"weight":0,"name":"Sale"},{"weight":0,"name":"Extended Size"},{"weight":0,"name":"Accessories and Shoes"},{"weight":0,"name":"Activewear"},{"weight":0,"name":"Disney Project"},{"weight":0,"name":"Dresses and Jumpsuits"},{"weight":0,"name":"Fleece"},{"weight":0,"name":"HEATTECH Collection"},{"weight":0,"name":"Ines de la Fressange"},{"weight":0,"name":"Intimates"},{"weight":0,"name":"Weekly Promotions"},{"weight":0,"name":"Loungewear"},{"weight":0,"name":"Outerwear and Blazers"},{"weight":0,"name":"Pants"},{"weight":0,"name":"Shirts and Blouses"},{"weight":0,"name":"Skirts"},{"weight":0,"name":"Socks and Hosiery"},{"weight":0,"name":"Sweaters"},{"weight":0,"name":"Sweatshirts and Sweatpants"},{"weight":0,"name":"T-Shirts and Tops"},{"weight":0,"name":"UT: Graphic Tees"},{"weight":0,"name":"Wear To Work"},{"weight":0,"name":"Mix and Match"}]
预期:当所有情况下权重值为0时,我希望结果与输入相同。
任何帮助表示感谢。
答案 0 :(得分:3)
你的问题是用JavaScript排序并不能保证稳定.ECMA-262规范说明了这一点:
此数组的元素已排序。排序不一定稳定(即,比较相等的元素不一定保持原始顺序)。
问题Fast stable sorting algorithm implementation in javascript包含指向稳定排序的this Vjeux blog post链接以及其他几个选项。
答案 1 :(得分:2)
Sorting with map的建议。这里有索引并使用第二个链式排序条件保留原始排序。
log4.debug('compare '+pwd+' with saved on db for user %j', userDoc.password);
var okPwd = bcrypt.compareSync(pwd, userDoc.password);
mapped.sort(function (a, b) {
return a.value - b.value || a.index - b.index;
});

// the array to be sorted
var data = [{ weight: 0, name: "New Arrivals" }, { weight: 0, name: "Weekly Promotions" }, { weight: 0, name: "Sale" }, { weight: 0, name: "Extended Size" }, { weight: 0, name: "Accessories and Shoes" }, { weight: 0, name: "Activewear" }, { weight: 0, name: "Disney Project" }, { weight: 0, name: "Dresses and Jumpsuits" }, { weight: 0, name: "Fleece" }, { weight: 0, name: "HEATTECH Collection" }, { weight: 0, name: "Ines de la Fressange" }, { weight: 0, name: "Intimates" }, { weight: 0, name: "Jeans" }, { weight: 0, name: "Loungewear" }, { weight: 0, name: "Outerwear and Blazers" }, { weight: 0, name: "Pants" }, { weight: 0, name: "Shirts and Blouses" }, { weight: 0, name: "Skirts" }, { weight: 0, name: "Socks and Hosiery" }, { weight: 0, name: "Sweaters" }, { weight: 0, name: "Sweatshirts and Sweatpants" }, { weight: 0, name: "T-Shirts and Tops" }, { weight: 0, name: "UT: Graphic Tees" }, { weight: 0, name: "Wear To Work" }, { weight: 0, name: "Mix and Match" }];
// temporary array holds objects with position and sort-value
var mapped = data.map(function (el, i) {
return {
index: i,
value: el.weight
};
});
// sorting the mapped array containing the reduced values
mapped.sort(function (a, b) {
return a.value - b.value || a.index - b.index;
});
// container for the resulting order
var result = mapped.map(function (el) {
return data[el.index];
});
console.log(result);

答案 2 :(得分:1)
最简单的解决方案是保存位置,然后在对象相等时进行比较:
var sortData = data.slice(0);
sortData.forEach(function(element, index){
element.index = index;
});
sortData.sort(function(a,b) {
var diff = a.weight - b.weight;
return diff === 0 ? a.index - b.index : diff;
});
sortData.forEach(function(element){
delete element.index;
});
答案 3 :(得分:1)
您正在寻找的是名为stability
的排序算法的属性。这意味着在排序后,等值元素的顺序保持不变。不同的浏览器使用不同的算法进行排序,其中一些浏览器使用稳定的,一些使用不稳定的算法。您可以在此SO线程上看到浏览器之间的差异:https://stackoverflow.com/a/3027715/1641070。
正如你在Ramda源代码中看到的那样,它使用了内置的排序机制,因此如果你使用Ramda,你会得到相同的结果:https://github.com/ramda/ramda/blob/v0.22.1/src/sortBy.js#L38:L42
答案 4 :(得分:0)
您可以使用下划线的_.sortBy(数据,'权重')进行稳定排序。 check this