-2

I have an array with objects that looks like this:

{
 Title: "Title",
 Url: "myUrl",
 publishedDate: "19/01/2021"
  },
  {
   Title: "Title 2",
Url: "myUrl",
publishedDate: "17/12/2020"
  },
  {
  Title: "Title 3",
Url: "myUrl",
publishedDate: "11/03/2021"
  },
  {
Title: "Title 4",
Url: "myUrl",
publishedDate: "11/12/2020"
  }

I have saved them in my state (array).

this.state.data

I want to order the objects based on the property "publishedDate" (and without using the new Date() due to memory efficiency). I try:

console.log(this.state.data.sort(function(a, b) {
  var c = new Date(a.publishedDate);
  var d = new Date(b.publishedDate);
  return c.getTime() - d.getTime();
}))

But the objects are not in order. What is wrong in the code? Playground:

https://www.typescriptlang.org/play?#code/FAYw9gdgzgLgBAQwE5IQTzgXjgb2HOAEwRgQC44BtPOAFQEsYAbAUwoCIHmX2AafAKpImHALZohTPvgAOAVwBGTelAAWLQgBESbOOwCMATgD0ABn3GATKcv72+OAF9+BGgS6sOHlnEvTJYhLC0vJKKupaOhz6AOzG+pZWNqb2BM4Obt5ejKxwAMz+woGSIYrKahraMLoGFqZ5SbapTi64wFl63nAALIUieuIl-KHlEVU1+hYJjSkOjg4AusDzwKCQUGCsAHRMYADmABTIqGhbxKRbG0gwBwBmchAgMPSQR7xwCgCUbQQAbshwEBYOAQFgAdzg4yOWxG4UqOk+AG4HP8kERgaCIVCFDCynDItUkQ4kCwYHIkBBAVs9qSGKIWAdvgBaIjU2n0emM5GOT6fYBAA

  • _"and without using the new Date() due to memory efficiency"_ - Why should `new Date()` be a problem? – Andreas Dec 18 '20 at 12:13
  • Because allocating memory inside of a sort function. Have been discussed here: https://stackoverflow.com/questions/10123953/how-to-sort-an-array-by-a-date-property – SharePointBeginner Dec 18 '20 at 12:14
  • What have you tried to solve this on your own? Splitting the date into its parts and comparing them shouldn't be that difficult – Andreas Dec 18 '20 at 12:15
  • Yeah you could do this, just write your own compareDates function and your good to go. Split the date in his 3 components using "/" as delimiter – Syder Dec 18 '20 at 12:17
  • There's exactly _one_ comment, without any further facts to prove its assumption. Without an exhaustive analysis with a profiler you really shouldn't think about "memory efficiency". – Andreas Dec 18 '20 at 12:18
  • It is true its only "one comment". It has its upvotes though, and remind me how "legitimacy" works, specifically for SO. – SharePointBeginner Dec 18 '20 at 12:20
  • Seriously. Don't bother with such things unless you used a profiler that proves your assumption that this exact spot is the bottleneck of your script. Anything else is just a waste of your time and energy. – Andreas Dec 18 '20 at 12:23
  • The data flow will not be constant, obviously - it may not be a problem right now, but maybe later on hence why I tend to think doing robust applications and rather "waste time now" to save "my time" later on, rather than the thinking "well, it works good now". In this case, its for a big clients SP intranet. – SharePointBeginner Dec 18 '20 at 12:30

2 Answers2

3

If the date format is always the same, you can just create the string in format YYYYMMDD and then compare -

var data = [{
  Title: "Title",
  Url: "myUrl",
  publishedDate: "19/01/2021"
}, {
  Title: "Title 2",
  Url: "myUrl",
  publishedDate: "17/12/2020"
}, {
  Title: "Title 3",
  Url: "myUrl",
  publishedDate: "11/03/2021"
}, {
  Title: "Title 4",
  Url: "myUrl",
  publishedDate: "11/12/2020"
}]

var sortedData = data.sort(function(a, b) {
  a = a.publishedDate.split('/').reverse().join('');
  b = b.publishedDate.split('/').reverse().join('');
  return a > b ? 1 : a < b ? -1 : 0;
});

console.log(sortedData);

The solution here also suggests the same (as well as to use .localeCompare as an alternative)

Nikhil Patil
  • 2,480
  • 1
  • 7
  • 20
1

You can use the Date.UTC method that accepts year, month index, day to create your dates and pass these values by parsing your input dates strings like so:

this.state =  { data: [{ Title: "Title", Url: "myUrl", publishedDate: "19/01/2021" }, { Title: "Title 2", Url: "myUrl", publishedDate: "17/12/2020" }, { Title: "Title 3", Url: "myUrl", publishedDate: "11/03/2021" }, { Title: "Title 4", Url: "myUrl", publishedDate: "11/12/2020" }]}; 

console.log(this.state.data.sort(function(a, b) {
  var c = Date.UTC(a.publishedDate.split("/")[2], a.publishedDate.split("/")[1] - 1, a.publishedDate.split("/")[0]);
  var d = Date.UTC(b.publishedDate.split("/")[2], b.publishedDate.split("/")[1] - 1, b.publishedDate.split("/")[0]);
  return c - d;
}))
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 1
    `Date.UTC()` doesn't change that much in regards to _"memory efficiency"_, but that "restriction" doesn't make sense at all in the first place... – Andreas Dec 18 '20 at 12:28
  • It's just one other way of doing it I guess, if the OP wishes to avoid new Date(), that's ok by me :-) – Terry Lennox Dec 18 '20 at 12:29
  • @Andreas "But that "restriction" doesn't make sense at all in the first place." I guess you have a good knowledge about my application. :) Dont understand why you are "policing" around like this, that makes no sense to me and its not the first time I see you like this. – SharePointBeginner Dec 18 '20 at 12:34