如何根据 id 对该数组进行排序?
const arr = [{
"id": 38938888,
"subInternalUpdates": true,
},
{
"id": 38938887,
"subInternalUpdates": true
},
{
"id": 38938889,
"subInternalUpdates": true
}
];
const sorted_by_name = arr.sort((a, b) => a.id > b.id);
console.log(sorted_by_name);Run Code Online (Sandbox Code Playgroud)
预期产出
const arr = [
{
"id": 38938887,
"subInternalUpdates": true
},
{
"id": 38938888,
"subInternalUpdates": true,
},
{
"id": 38938889,
"subInternalUpdates": true
}
];
Run Code Online (Sandbox Code Playgroud)
a.id - b.id当你订购数组时更好的回报:
const arr = [{
"id": 38938888,
"subInternalUpdates": true,
},
{
"id": 38938887,
"subInternalUpdates": true
},
{
"id": 38938889,
"subInternalUpdates": true
}
];
const sorted_by_name = arr.sort((a, b) => {
return a.id - b.id;
});
console.log(sorted_by_name);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4173 次 |
| 最近记录: |