我试图理解为什么 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 文档,但我要么错过了一些东西,要么没有完全理解它。任何帮助都是极好的。谢谢!
最佳答案
小写字母和大写字母有不同的代码,大写字母在前,小写字母在前。如果您想要不区分大小写的排序,您可以将格式化程序函数作为 iteratee 传递给 orderBy
:
const sortedData = _.orderBy(data, [data => data.name.toLowerCase()], ['asc']);
console.log(sortedData);
关于javascript - Lodash 按字母顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59058725/