【发布时间】:2019-11-26 20:14:17
【问题描述】:
我试图理解为什么 lodash orderBy 函数对这些的排序不正确。 (我想在我的大脑中是错误的)。
const data = [
{
id: '1',
name: 'FLUoxetine (FLUoxetine 20 mg oral capsule)'
},
{
id: '2',
name: 'ascorbic acid (Vitamin C 25 mg oral tablet, chewable)'
}
];
const orderedData = _.orderBy(data, ["name"], ["asc"]);
console.log("DATA>>>>", orderedData);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
以上代码正在退出
DATAZZZZ
[Object, Object]
0: Object
id: "1"
name: "FLUoxetine (FLUoxetine 20 mg oral capsule)"
1: Object
id: "2"
name: "ascorbic acid (Vitamin C 25 mg oral tablet, chewable)"
当我认为它会像这样注销时
DATAZZZZ
[Object, Object]
0: Object
id: "2"
name: "ascorbic acid (Vitamin C 25 mg oral tablet, chewable)"
1: Object
id: "1"
name: "FLUoxetine (FLUoxetine 20 mg oral capsule)"
谁能解释一下为什么?我假设它与前 3 个字母大写有关。我阅读了 lodash 文档,但我要么错过了一些东西,要么没有完全理解它。任何帮助都是极好的。谢谢!
【问题讨论】:
-
不区分大小写。
-
在 JS 图表中,大写字符在小写之前,所以
'a' < 'F' === false,但'A' < 'F' === true。如果您不想更改对象,则需要使用自己的回调。
标签: javascript function lodash