代码之家  ›  专栏  ›  技术社区  ›  bier hier

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

  •  1
  • bier hier  · 技术社区  · 7 年前

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

    _.sortBy
      (persons.entities.alerts,
      dateObj => new Date(dateObj.createdDateTime)
    ).reverse()
    

    这是数组:

    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"
          }
        ]
      }
    
    }
    

    排序返回按日期描述的正确顺序。在此示例中,日期相同。只有在这种情况下,我想按(申请人)姓氏排序,其中isprimaryApplication=true?链接到 codepen

    2 回复  |  直到 7 年前
        1
  •  1
  •   HMR    7 年前

    我很想用洛达什来做这个,但是文档并不能反映实际情况。sortby的第二个参数是文档中的一个数组,但如果我传递一个函数数组,它将不起作用。

    您可以向通知中添加名称,并在通知中添加用于排序的排序日期:

    const 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":["16671520041"],"id":"19d0da63-dfd0-4c00-a13a-cc822fc83869"},{"createdDateTime":"2019-06-14T00:00:00.000Z","applicants":["16671520040"],"id":"c5385595-2104-409d-a676-c1b57346f63e"}]}}
    
    const applicantsById = persons.entities.applicants.reduce(
        (result, applicant) => result.set(applicant.id, applicant),
        new Map(),
    );
    const alertsWithName = persons.entities.alerts.map((alert) => ({
        ...alert,
        sortDate:new Date(alert.createdDateTime).getTime(),
        name: (alert.applicants
            .map((id) => applicantsById.get(id))
            .filter((x) => x) //remove empty
            .find((applicant)=>applicant.isPrimaryApplicant)||{lastName:''}).lastName
    }));
    
    //according to not correct lodash documentation here:
    //https://lodash.com/docs/4.17.11#sortBy
    //we should be able to do this:
    // console.log(
    //   _.sortBy(alertsWithName, [
    //     (alert) => new Date(alert.createdDateTime),
    //     (alert) => alert.name,
    //   ])
    // )
    //however that's not going to work so can try array sort method
    
    console.log(
      alertsWithName.sort(
        (a,b)=>b.sortDate-a.sortDate || a.name.localeCompare(b.name)
      )
    )
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
        2
  •  2
  •   Rakesh Makluri    7 年前

    Loadash Sortby不提供比较器功能的选项(尽管有其他方法实现它)。

    您可以使用数组排序方法来实现这一点:

    persons.entities.alerts.sort(function(a1, a2) { 
        if(a1.createdDateTime === a2.createdDateTime) {
            let applicant1 = persons.entities.applicants.find(a => a.id === a1.applicants[0]);
            let applicant2 = persons.entities.applicants.find(a => a.id === a2.applicants[0]);
            if (!applicant1.isPrimaryApplicant || applicant1.lastName < applicant2.lastName) {
               return -1;
           }
           return 1;
    
        } else {
            let d1 = new Date(a1.createdDateTime);
            let d2 = new Date(a2.createdDateTime);
            return d2 - d1;
        }
    })