我需要根据布尔属性对数组中的对象进行排序.我的代码有效,但它似乎不是正确的方法,我无法弄清楚为什么.
const todos = [{
text: 'Check emails for the day',
completed: true
},{
text: 'Walk the dog',
completed: true
},{
text: 'Go to the store for groceries',
completed: false
},{
text: 'Pick up kids from school',
completed: false
},{
text: 'Do online classes',
completed: false
}]
const sortTodos = function (todos) {
todos.sort(function (a,b) {
if (a.completed < b.completed) { // (or) a.completed === false && b.completed === true
return -1
} else if (b.completed < a.completed){ // (or) …Run Code Online (Sandbox Code Playgroud)javascript ×1