JS对对象进行排序
2019-08-14 本文已影响0人
余霜序
// var obj =
var persons = [
{name:"lily1",sex:"女",age:18},
{name:"lily2",sex:"女",age:19},
{name:"lily3",sex:"女",age:11},
{name:"lily4",sex:"女",age:28},
{name:"lily5",sex:"女",age:22},
]
//请对数组persons进行按照年龄升序排序
persons.sort(function (a,b) {
if (a.age>b.age) {
return 1;
}else if(a.age<b.age){
return -1;
}else{
return 0;
}
})
console.log(persons);