-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

8
  • "and without using the new Date() due to memory efficiency" - Why should new Date() be a problem? Commented Dec 18, 2020 at 12:13
  • Because allocating memory inside of a sort function. Have been discussed here: stackoverflow.com/questions/10123953/… Commented Dec 18, 2020 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 Commented Dec 18, 2020 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 Commented Dec 18, 2020 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". Commented Dec 18, 2020 at 12:18

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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;
}))

3 Comments

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...
It's just one other way of doing it I guess, if the OP wishes to avoid new Date(), that's ok by me :-)
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.