我有一个对象数组,每个对象上都有state
个值。
我想对数组进行排序,以便任何状态为healthy
或unhealthy
的对象都被推送到数组的末尾。
我想将所有healthy
个项目和所有unhealthy
个项目组合在一起。
var people = [
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'other'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'unhealthy'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'other'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'healthy'
}
{
name: 'XXX',
state: 'other'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'other'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'unhealthy'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'healthy'
}
];
我希望最终的数组看起来像这样:
var people = [
{
name: 'XXX',
state: 'other'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'other'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'other'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'other'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'dead'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'healthy'
},
{
name: 'XXX',
state: 'unhealthy'
},
{
name: 'XXX',
state: 'unhealthy'
}
];
我尝试过以下操作,但出于某种原因,数组中的某些项目会被遗漏。
我认为可能因为我将物品推到底部,所有其他物品的索引都会发生变化,而我却错过了一些物品?
_.each(people, function(p){
if(p.state === 'healthy'){
var arr_idx = people.indexOf(p);
people.push(people.splice(arr_idx, 1)[0]);
}
});
_.each(people, function(p){
if(p.state === 'unhealthy'){
var arr_idx = people.indexOf(p);
people.push(people.splice(arr_idx, 1)[0]);
}
});
答案 0 :(得分:2)
您可以使用Array.prototype.sort():
var people = [{name: 'XXX',state: 'other'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'other'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'other'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'other'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'unhealthy'},{name: 'XXX',state: 'unhealthy'}];
people.sort(function(a, b) {
return (a.name === 'healthy') ? -1 : ((b.name === 'unhealthy') ? -2 : 0);
});
console.log(people);
.as-console-wrapper{top:0;max-height:100% !important;}
答案 1 :(得分:2)
您可以使用对象作为排序顺序。
var people = [{ name: 'XXX', state: 'healthy' }, { name: 'XXX', state: 'other' }, { name: 'XXX', state: 'healthy' }, { name: 'XXX', state: 'dead' }, { name: 'XXX', state: 'unhealthy' }, { name: 'XXX', state: 'healthy' }, { name: 'XXX', state: 'dead' }, { name: 'XXX', state: 'healthy' }, { name: 'XXX', state: 'dead' }, { name: 'XXX', state: 'other' }, { name: 'XXX', state: 'dead' }, { name: 'XXX', state: 'healthy' }, { name: 'XXX', state: 'other' }, { name: 'XXX', state: 'healthy' }, { name: 'XXX', state: 'dead' }, { name: 'XXX', state: 'other' }, { name: 'XXX', state: 'dead' }, { name: 'XXX', state: 'unhealthy' }, { name: 'XXX', state: 'dead' }, { name: 'XXX', state: 'healthy' }];
people.sort(function (a, b) {
var order = { healthy: 1, unhealthy: 2 }; // easy to maintain
return (order[a.state] || 0) - (order[b.state] || 0); // return delta of order
});
console.log(people);

答案 2 :(得分:1)
使用Array.prototype.sort
对people
数组进行排序,如下所示:
var people = [{name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'other'}, {name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state:'unhealthy'}, {name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state:'healthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'other'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'other'}, {name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'other'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'unhealthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'healthy'}];
people.sort(function(a, b) {
if(a.state === 'healthy' ||
a.state === 'unhealthy')
return 1;
else
return -1;
});
console.log(people);

.as-console-wrapper{top:0;max-height:100% !important;}

答案 3 :(得分:0)
您还可以使用array.sort
订购价值
var people = [{name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'other'}, {name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state:'unhealthy'}, {name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state:'healthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'other'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'other'}, {name: 'XXX',state: 'healthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'other'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'unhealthy'}, {name: 'XXX',state: 'dead'}, {name: 'XXX',state: 'healthy'}];
var orderMap = {
'healthy': 10,
'unhealthy': 11,
}
people.sort(function(a,b){
var _a = a.state;
var _b = b.state;
var o_a = orderMap[_a];
var o_b = orderMap[_b];
if(o_a) return 1;
else if (o_b) return -1;
else return _a > _b ? 1 : _a < _b ? -1 : 0
});
console.log(people)
答案 4 :(得分:0)
试试这个:
var people = [{name: 'XXX',state: 'other'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'other'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'other'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'other'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'dead'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'healthy'},{name: 'XXX',state: 'unhealthy'},{name: 'XXX',state: 'unhealthy'}];
console.log(_.sortBy(people,function(p, index){
return p.name == 'healthy' ? -1 : ( p.name == 'unhealthy' ? -2 : index);
}))
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
答案 5 :(得分:0)
一个简单的比较函数应该可以解决这个问题。
function test(a,b) {
if (a.state === b.state) return 0;
if (a.state === 'unhealthy') return .5;
if (a.state === 'healthy') return 1;
return -1
}
var sortedArray = people.sort(test);
这是一个例子。