我有一个变量和对象数组,例如:
var selectedName = 'fff';
[{
percentage: Math.round(percentage),
count: count,
name: 'bbb',
index: 11,
},
{
percentage: Math.round(percentage),
count: 200,
name: 'aaa',
index: 2,
},
{
percentage: Math.round(percentage),
count: 400,
name: 'All',
index: 7,
},
{
percentage: Math.round(percentage),
count: count,
name: 'fff',
index: 8,
},
{
percentage: Math.round(percentage),
count: count,
name: 'ccc',
index: 3,
}],
我想按如下方式对它们进行排序:名称为' All'永远应该是第一位的。下一个对象应该是名称与selectedName
匹配的对象,在这种情况下' fff'。其余的对象应该按照他们的索引的升序排序'属性。
这可以通过一个Array.sort()方法实现吗?
答案 0 :(得分:4)
是的,这是一个例子:
var selectedName = 'fff';
let percentage = 1;
let count = 2;
var data=[{percentage:Math.round(percentage),count:count,name:"bbb",index:11},{percentage:Math.round(percentage),count:200,name:"aaa",index:2},{percentage:Math.round(percentage),count:400,name:"All",index:7},{percentage:Math.round(percentage),count:count,name:"fff",index:8},{percentage:Math.round(percentage),count:count,name:"ccc",index:3}];
arr.sort((i, j) => {
if (i.name === j.name && (i.name === 'All' || i.name === selectedName)) return 0;
if (i.name === 'All') return -1;
if (j.name === 'All') return 1;
if (i.name === selectedName) return -1;
if (j.name === selectedName) return 1;
if (i.index < j.index) return -1;
if (i.index > j.index) return 1;
return 0;
})
console.log(arr)
&#13;
答案 1 :(得分:1)
当然你可以这样做:
var selectedName = 'fff';
var percentage = 23;
var count = 3;
var data=[{percentage:Math.round(percentage),count:count,name:"bbb",index:11},{percentage:Math.round(percentage),count:200,name:"aaa",index:2},{percentage:Math.round(percentage),count:400,name:"All",index:7},{percentage:Math.round(percentage),count:count,name:"fff",index:8},{percentage:Math.round(percentage),count:count,name:"ccc",index:3}];
function sortItems(a, b) {
if (a.name === 'All') {
return -1
}
if (b.name === 'All') {
return 1;
}
if (a.name === selectedName) {
return -1
}
if (b.name === selectedName) {
return 1
}
return a.index - b.index;
}
console.log(items.sort(sortItems));
答案 2 :(得分:1)
您可以尝试这样的事情:
(a.name !== priorityName)
将产生一个布尔值。对它们使用-
(减号)运算符时,它将转换为数值(true:1,false:0)。1
,输出将为0
,这在JS中是假的。
var selectedName = 'fff';
var priorityName = "All";
var percentage = 50.4, count= 0;
var data=[{percentage:Math.round(percentage),count:count,name:"bbb",index:11},{percentage:Math.round(percentage),count:200,name:"aaa",index:2},{percentage:Math.round(percentage),count:400,name:"All",index:7},{percentage:Math.round(percentage),count:count,name:"fff",index:8},{percentage:Math.round(percentage),count:count,name:"ccc",index:3}];
data.sort(function(a,b){
return (a.name !== priorityName) - (b.name !== priorityName) ||
(a.name !== selectedName) - (b.name !== selectedName) ||
a.index - b.index;
})
console.log(data)
答案 3 :(得分:1)
您可以使用包含每个元素优先级的帮助对象。在您的特定情况下,最高优先级的值为All
。
首先,我们会对优先级较高的(All, 'fff')
值进行排序,一旦完成,我们会按index
值对其余值进行排序。
注意:由于我们按升序对其进行排序,因此优先级值必须为负数。如果我们按降序(b - a)
对它进行排序,那么它们就是正面的。
var arr = [{percentage:Math.round("percentage"),count:"count",name:"bbb",index:11},{percentage:Math.round("percentage"),count:200,name:"aaa",index:2},{percentage:Math.round("percentage"),count:400,name:"All",index:7},{percentage:Math.round("percentage"),count:"count",name:"fff",index:8},{percentage:Math.round("percentage"),count:"count",name:"ccc",index:3}],
selectedName = 'fff',
result = arr.sort(function(a,b){
var order = {All: -2, [selectedName]: -1, default: 0};
return (order[a.name] || order.default) - (order[b.name] || order.default) || a.index - b.index;
});
console.log(result);
&#13;