Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort by name descending in lodash?

I have an array of objects that are sorted in descending order by date:

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

This is the array:

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

}

The sort returns the correct order by date desc. In this sample the dates are the same. Only in this case i want to sort by (applicants) lastName where isPrimaryApplicant=true? Link to codepen

like image 399
bier hier Avatar asked Jan 23 '19 09:01

bier hier


People also ask

How do you sort in descending order in Lodash?

If orders is unspecified, all values are sorted in ascending order. Otherwise, specify an order of "desc" for descending or "asc" for ascending sort order of corresponding values.

How do I sort in Lodash?

The _. sortBy() method creates an array of elements which is sorted in ascending order by the results of running each element in a collection through each iteratee. And also this method performs a stable sort which means it preserves the original sort order of equal elements.

Does Lodash groupBy preserve order?

groupBy , but it does preserve the order of array-like collections, and that's probably unlikely to change. So the sub-items within groups would retain their original ordering, but the grouped key ordering may change, because they are object properties.

Is Lodash sortBy stable?

Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru each iteratee. This method performs a stable sort, that is, it preserves the original sort order of equal elements.


1 Answers

You want lodash's orderBy, which allows sort directions.

You can attach asc or desc to each sort property you use.

This should get you the ordering you're looking for:

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

like image 200
Tony Brasunas Avatar answered Sep 21 '22 08:09

Tony Brasunas