我有一组具有多个属性的对象。给出以下数组:
var people = [
{name: "allen", age: 33, color:"green"},
{name: "jon", age: 23, color:"blonde"},
{name: "silver", age: 54, color:"yellow"},
{name: "james", age: 52, color:"grey"},
{name: "flint", age: 25, color:"pink"},
{name: "beilly", age: 31, color:"blonde"},
{name: "bwones", age: 47, color:"grey"},
{name: "sas", age: 35, color:"green"},
{name: "jackson", age: 234, color:"yellow"},
{name: "leonsardo", age: 12, color:"brown"},
{name: "dicaeprio", age: 73, color:"pink"},
{name: "sylvfester", age: 35, color:"blonde"},
{name: "alleen2", age: 33, color:"green"},
{name: "jofn2", age: 23, color:"blonde"},
{name: "sdilver2", age: 54, color:"yellow"},
{name: "jamaes2", age: 52, color:"grey"}
];
我需要按color
属性对此数组进行排序,但需要以特殊方式排序,首先是green
,然后是yellow
,然后是brown
,然后是{{ 1}},然后是pink
,最后是grey
。我读过here和here,但很难根据我的需要生成压缩机。由于这只是一个演示阵列而我的真实数据将是一个更大的数组,因此排序机制应该比 blonde
更快。
答案 0 :(得分:7)
这是你的比较器
var sortOrder = {green: 0, yellow: 1, brown: 2, pink: 3, grey: 4, blonde: 5};
people.sort(function (p1, p2) {
return sortOrder[p1.color] - sortOrder[p2.color];
});
答案 1 :(得分:2)
我建议使用默认值进行排序,具体取决于未列出的颜色应该排序的位置。
在这种情况下,排序顺序对象的属性必须以大于零的值开始。
colorOrder = { green: 1, yellow: 2, brown: 3, pink: 4, grey: 5, blonde: 6 };
people.sort(function (a, b) {
return (colorOrder[a.color] || 0) - (colorOrder[b.color] || 0);
});
答案 2 :(得分:1)
使用Andrey方法,在对象中只添加一个参数:
var sortOrder = {green: 0, yellow: 1, brown: 2, pink: 3, grey: 4, blonde: 5};
people.sort(function (p1, p2) {
return sortOrder[p1.color] - sortOrder[p2.color];
});
或如果你真的不能使用,请创建排序功能:
var people =
[
{name: "allen", age: 33, color:"green"},
{name: "jon", age: 23, color:"blonde"},
{name: "silver", age: 54, color:"yellow"},
{name: "james", age: 52, color:"grey"},
{name: "flint", age: 25, color:"pink"},
{name: "beilly", age: 31, color:"blonde"},
{name: "bwones", age: 47, color:"grey"},
{name: "sas", age: 35, color:"green"},
{name: "jackson", age: 234, color:"yellow"},
{name: "leonsardo", age: 12, color:"brown"},
{name: "dicaeprio", age: 73, color:"pink"},
{name: "sylvfester", age: 35, color:"blonde"},
{name: "alleen2", age: 33, color:"green"},
{name: "jofn2", age: 23, color:"blonde"},
{name: "sdilver2", age: 54, color:"yellow"},
{name: "jamaes2", age: 52, color:"grey"}
];
var order = ['green','yellow','brown','pink','grey','blonde'];
function mySort(array)
{
var list = [];
function getElem(array,id)
{
for(var i in array) if(array[i].color == id) list.push(array[i])
}
for(var i in order) getElem(array,order[i]);
return list;
}
mySort(people);
答案 3 :(得分:1)
我认为执行此工作的正确方法是使用哈希和排序,但不使用排序,以下代码可能会变得非常有效。
var people = [
{name: "allen", age: 33, color:"green"},
{name: "jon", age: 23, color:"blonde"},
{name: "silver", age: 54, color:"yellow"},
{name: "james", age: 52, color:"grey"},
{name: "flint", age: 25, color:"pink"},
{name: "beilly", age: 31, color:"blonde"},
{name: "bwones", age: 47, color:"grey"},
{name: "sas", age: 35, color:"green"},
{name: "jackson", age: 234, color:"yellow"},
{name: "leonsardo", age: 12, color:"brown"},
{name: "dicaeprio", age: 73, color:"pink"},
{name: "sylvfester", age: 35, color:"blonde"},
{name: "alleen2", age: 33, color:"green"},
{name: "jofn2", age: 23, color:"blonde"},
{name: "sdilver2", age: 54, color:"yellow"},
{name: "jamaes2", age: 52, color:"grey"}
],
arrays = [green, yellow, brown, pink, grey, blonde] = [[],[],[],[],[],[]],
result = [];
Object.keys(people).forEach(k => this[people[k].color].push(people[k]));
result = arrays.reduce((p,c) => p.concat(c));
console.log(result);
为了提高性能,您可以用
替换最后一行result = arrays.reduce((p,c) => (Array.prototype.push.apply(p,c),p));