const list = [ { name: 'John', age: 25 }, { name: 'Alice', age: 30 }, { name: 'Bob', age: 20 } ]; list.sort((a, b) => { // 首先按照 name 属性进行升序排序 if (a.name > b.name) { return 1; } else if (a.name < b.name) { return -1; } // 如果 name 属性相同,则按照 age 属性进行升序排序 return a.age - b.age; });
标签:return,name,age,TS,数组,升序,排序,属性 From: https://www.cnblogs.com/happen-/p/17534320.html