【发布时间】:2019-03-24 09:44:15
【问题描述】:
当我想解决 JavaScript* 中的排序问题时,我发现 false Boolean 比 true Boolean 差。为什么?
我已经尝试了以下内容:
console.log(false < true) //return true
console.log(true < false) //return false
我的问题:
const todos = [{
text: 'running',
completed: false
}, {
text: 'walking',
completed: true
}, {
text: 'Studying',
completed: true
}, {
text: 'learn javascript',
completed: false
}, {
text: 'learn react',
completed: true
}]
const sortTodos = function(todo) {
todo.sort(function(a, b) {
if (a.completed < b.completed) {
return -1
} else if (b.completed < a.completed) {
return 1
} else {
return 0
}
})
}
sortTodos(todos)
console.log(todos)
【问题讨论】:
-
因为布尔值 true 被解释为 1,false 被解释为 0
标签: javascript arrays boolean