【发布时间】:2020-07-07 14:05:36
【问题描述】:
搜索了许多我没有设法解决我的案例的例子。我有一个数组
应该按author 排序。我的尝试什么也没做。什么是正确的表达方式?
var array = [
["ifke", {
"title": "secure",
"author": "admin"
}],
["lottery", {
"title": "The lo",
"author": "anton"
}],
["short-content", {
"title": "Short Content",
"author": "scott"
}],
["ziddler", {
"title": "The Fiddler",
"author": "herman"
}]
];
array.sort((a, b) => a[1].author - b[1].author)
console.log(array)
【问题讨论】:
-
好吧,在尝试回答之前,我可以问一下为什么您的数据是这样构造的吗?太可怕了。因为如果这不仅仅是家庭作业,那么我强烈建议您重新考虑存储数据的方式
-
试试
array.sort((a, b) => a[1].author.localeCompare(b[1].author)) -
感谢您的建议。数据结构来自 [import function loading markdown files into a JSON] (npmjs.com/package/markdown-to-json) 对象。现在我的目标是按照描述返回这个排序。
标签: javascript sorting