如何在lodash中按名称降序排序?

bie*_*ier 4 javascript lodash

我有一个按日期降序排序的对象数组:

_.sortBy
  (persons.entities.alerts,
  dateObj => new Date(dateObj.createdDateTime)
).reverse()
Run Code Online (Sandbox Code Playgroud)

这是数组:

let persons={
  "entities": {
    "applicants": [
      {
        "lastName": "Agamemnon",
        "isPrimaryApplicant": true,
        "id": "16671520038"
      },
      {
        "lastName": "Purdy",
        "isPrimaryApplicant": false,
        "id": "16671520039"
      },
      {
        "lastName": "Brekky",
        "isPrimaryApplicant": true,
        "id": "16671520040"
      },
      {
        "lastName": "Tabouli",
        "isPrimaryApplicant": true,
        "id": "16671520041"
      }
    ],
    "alerts": [
      {
        "createdDateTime": "2018-06-14T00:00:00.000Z",
        "applicants": ["16671520038", "16671520039"],
        "id": "05025fea-ec37-4767-a868-a646597365d0"
      },
      {
        "createdDateTime": "2018-06-14T00:00:00.000Z",
        "applicants": ["16671520040"],
        "id": "19d0da63-dfd0-4c00-a13a-cc822fc83869"
      },
      {
        "createdDateTime": "2018-06-14T00:00:00.000Z",
        "applicants": ["16671520041"],
        "id": "c5385595-2104-409d-a676-c1b57346f63e"
      }
    ]
  }

}
Run Code Online (Sandbox Code Playgroud)

排序按日期 desc 返回正确的顺序。在此示例中,日期是相同的。只有在这种情况下,我想按(申请人)姓氏排序,其中 isPrimaryApplicant=true?代码笔链接

Ton*_*nas 7

您需要 lodash 的orderBy,它允许排序方向。

您可以将asc或附加desc到您使用的每个排序属性。

这应该可以为您提供您正在寻找的订单:

_.orderBy(persons.entities.applicants, ['lastName'], ['desc'])