【发布时间】:2021-09-05 02:44:16
【问题描述】:
按字母排序的最佳方法是什么,然后是 javascript/node 中的符号?我在下面使用此函数按字母顺序对其进行排序,但是“_text”在顶部排序。
const items = {
"objectb": "text",
"objecta": "one",
"_text": "two",
"objectc": "three"
}
const ordered = Object.keys(items).sort().reduce(
(obj, key) => {
obj[key] = items[key];
return obj;
}, {}
);
// This produces the sorted object, however the symbol key is sorted at the top, whereas I would like it at the bottom.
RETURNS:
{
"_text": "text",
"objecta": "one",
"objectb": "two",
"objectc": "three"
}
WOULD LIKE:
{
"objecta": "one",
"objectb": "two",
"objectc": "three",
"_text": "text"
}
【问题讨论】:
-
将您自己的排序函数作为参数传递给sort
-
如果您关心订单,请不要使用对象。
标签: javascript node.js sorting