user3872094
user3872094

Reputation: 3351

Unable to sort array using a date criteria

I'm writing a program where I need to sort an array based on createddate as criteria and my code is as below.

var carrData = [{
    "id": "3ae8f701-e652-45d3-94e8-05bc678cb465",
    "contactedByType": "Heather Livingston",
    "noteText": "private",
    "createdDate": 1626140883066
  },
  {
    "id": "cb044aca-76ec-478a-9cb7-60256887ede0",
    "contactedByType": "Carla Joy Hunter",
    "noteText": "sgdh",
    "createdDate": 1625936505039
  },
  {
    "id": "59c87e34-98b6-4e1c-959e-cfb289add58b",
    "contactedByType": "Carla Joy Hunter",
    "noteText": "testst",
    "createdDate": 1625936386438
  },
  {
    "id": "d42ae70c54168cc0c032ef14ec44e047",
    "createdDate": 1613433600000,
    "noteText": "Provided another ",
    "contactedByType": "Heather Livingston"
  },
  {
    "id": "8de4071beb5345a50dd75c6b677880f6",
    "createdDate": 1600387200000,
    "noteText": "2nd and 3rd cover letter reviews",
    "contactedByType": "Heather Livingston"
  },
  {
    "id": "92a99e56d2ec44950ad7c78d8b2ce9c3",
    "createdDate": 1600300800000,
    "noteText": "3rd resume review refining content details.",
    "contactedByType": "Heather Livingston"
  },
  {
    "id": "83732d5d127d725dd154b0ea5b6a9aa1",
    "createdDate": 1600128000000,
    "noteText": "Provided detailed",
    "contactedByType": "Heather Livingston"
  },
  {
    "id": "002012808dc76e6521969189e9bce289",
    "createdDate": 1599004800000,
    "noteText": "written feedback on cover letter.",
    "contactedByType": "Heather Livingston"
  },
  {
    "id": "ad780ec71ae843bdd26c7591d973b82c",
    "createdDate": 1599004800000,
    "noteText": "2nd rr",
    "contactedByType": "Heather Livingston"
  },
  {
    "id": "b8078ec31a96091aab0524c10ad09e4a",
    "createdDate": 1598918400000,
    "noteText": "Overview on the phone",
    "contactedByType": "Heather Livingston"
  },
  {
    "id": "1e7b0b042385c1b060f64f46651fbbb3",
    "createdDate": 1596672000000,
    "noteText": "Follow up",
    "contactedByType": "Heather Livingston"
  },
  {
    "id": "1153ef94e1056678a6ffebc689d3cfbc",
    "createdDate": 1596067200000,
    "noteText": "Going to BS in ",
    "contactedByType": "Heather Livingston"
  }
];

carrData = carrData.sort((a, b) => b.createdDate - a.createdDate);
console.log(carrData);

Here I get the output same as Input. How can I fix this?

The criteria I'm looking for is sort based on createdDate latest to oldest.

Upvotes: 1

Views: 73

Answers (1)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48751

There are two data entries with non-unique creation dates (1599004800000):

{
  "id": "ad780ec71ae843bdd26c7591d973b82c",
  "createdDate": 1599004800000,
  "noteText": "2nd rr",
  "contactedByType": "Heather Livingston"
}
{
  "id": "002012808dc76e6521969189e9bce289",
  "createdDate": 1599004800000,
  "noteText": "written feedback on cover letter.",
  "contactedByType": "Heather Livingston"
}

You need to also sort the note text ascending to make the sort more consistent.

const main = () => {
  console.log(`Data length: ${carrData.length}`);
  console.log(`Unique dates: ${new Set(carrData.map(({createdDate: d}) => d)).size}`);

  const sorted = carrData.sort((a, b) =>
    b.createdDate - a.createdDate || a.noteText.localeCompare(b.noteText));

  console.log(sorted);
};

const carrData = [{
  "id": "3ae8f701-e652-45d3-94e8-05bc678cb465",
  "contactedByType": "Heather Livingston",
  "noteText": "private",
  "createdDate": 1626140883066
}, {
  "id": "cb044aca-76ec-478a-9cb7-60256887ede0",
  "contactedByType": "Carla Joy Hunter",
  "noteText": "sgdh",
  "createdDate": 1625936505039
}, {
  "id": "59c87e34-98b6-4e1c-959e-cfb289add58b",
  "contactedByType": "Carla Joy Hunter",
  "noteText": "testst",
  "createdDate": 1625936386438
}, {
  "id": "d42ae70c54168cc0c032ef14ec44e047",
  "createdDate": 1613433600000,
  "noteText": "Provided another ",
  "contactedByType": "Heather Livingston"
}, {
  "id": "8de4071beb5345a50dd75c6b677880f6",
  "createdDate": 1600387200000,
  "noteText": "2nd and 3rd cover letter reviews",
  "contactedByType": "Heather Livingston"
}, {
  "id": "92a99e56d2ec44950ad7c78d8b2ce9c3",
  "createdDate": 1600300800000,
  "noteText": "3rd resume review refining content details.",
  "contactedByType": "Heather Livingston"
}, {
  "id": "83732d5d127d725dd154b0ea5b6a9aa1",
  "createdDate": 1600128000000,
  "noteText": "Provided detailed",
  "contactedByType": "Heather Livingston"
}, {
  "id": "002012808dc76e6521969189e9bce289",
  "createdDate": 1599004800000,
  "noteText": "written feedback on cover letter.",
  "contactedByType": "Heather Livingston"
}, {
  "id": "ad780ec71ae843bdd26c7591d973b82c",
  "createdDate": 1599004800000,
  "noteText": "2nd rr",
  "contactedByType": "Heather Livingston"
}, {
  "id": "b8078ec31a96091aab0524c10ad09e4a",
  "createdDate": 1598918400000,
  "noteText": "Overview on the phone",
  "contactedByType": "Heather Livingston"
}, {
  "id": "1e7b0b042385c1b060f64f46651fbbb3",
  "createdDate": 1596672000000,
  "noteText": "Follow up",
  "contactedByType": "Heather Livingston"
}, {
  "id": "1153ef94e1056678a6ffebc689d3cfbc",
  "createdDate": 1596067200000,
  "noteText": "Going to BS in ",
  "contactedByType": "Heather Livingston"
}];

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }

Upvotes: 3

Related Questions