lodash:按键进行主要排序,按键进行次要排序

时间:2019-02-14 14:16:28

标签: ecmascript-6 lodash

我有以下格式的数据。我目前正在使用orderBy by来按特定的“数字”键进行排序。我正在寻找一种通过另一个键“名称”进行辅助的方法。

[
{
  "number": "123",
  "name": "a"
},
{
  "number": "123",
  "name": "b"
},
{
  "number": "123",
  "name": "br"
}
]

1 个答案:

答案 0 :(得分:0)

您可以使用orderBy()

# from master :
git merge 123aeca
#   test
git merge 234eacb
#   test
git merge 345eacc
#   test
git merge feature_branch
#   test
const arr = [
  {number: 123, name: "b"},
  {number: 123, name: "a"},
  {number: 122, name: "br"},
  {number: 124, name: "bR"},
  {number: 124, name: "Br"}
];

console.log(
  _.orderBy(arr, e => [e.number, e.name.toLowerCase() /* case insensitively */])
);

const arr2 = [
  {number: 121, name: "c"},
  {number: 122, name: "b"},
  {number: 123, name: "a"}
];

console.log(
  _.orderBy(arr2, e => e.number /* descending */, 'desc')
);