0

I need to sort this array of objects by its nested property in descending order but the property that I need to sort by is "stored" inside an property index (not sure if thats what's called). How do i do this with .sort()?

Ive tried searching for the answer on stackoverflow and there are alot of topics on this but I can't find this specific answer or I can't understand it..

I have tried this code:

        var array = [
          {user:"Julia", startTime: "2019-04-09T11:22:36"}, 
          {user:"Lisa", startTime:"2019-04-10T11:22:36"},
          {user:"Hank", startTime:"2019-04-11T11:22:36"},
          {user:"Hank", startTime:"2019-04-08T11:22:36"},
        ];
        
        
        function compare(a, b) {
          const startA = new Date(a.startTime).getTime();
          const startB = new Date(b.startTime).getTime();
          return startA + startB;
        }
        
        console.log(array.sort(compare));

5
  • If you wanted an array, you should leave out they key: syntax, that's for objects Commented Apr 13, 2019 at 8:41
  • sry, this was my first post. Where exactly did I go wrong? Commented Apr 13, 2019 at 9:03
  • Just run the snippet - you can see that the syntax is incorrect. You didn't define the array properly Commented Apr 13, 2019 at 9:14
  • ah I see. I fixed the syntax, but it didnt solve the problem I had. Like you said the syntax was wrong but the problem remained. Fortunately Code Maniac & topheman corrected the syntax and gave me the answer. Commented Apr 13, 2019 at 12:56
  • The answer is in the question that I linked to - see the top of the page Commented Apr 13, 2019 at 13:09

3 Answers 3

2

return startB - startA; instead of return startA + startB;.

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

2 Comments

I cant see how this is the answer as I wanted them in descending order? Or am I failing to see something?
Oh, sorry. My mistake.
0

You were close: just change return startA + startB; to return startA > startB ? -1 : 1;

var array = [
  {user:"Julia", startTime: "2019-04-09T11:22:36"}, 
  {user:"Lisa", startTime:"2019-04-10T11:22:36"},
  {user:"Hank", startTime:"2019-04-11T11:22:36"},
  {user:"Hank", startTime:"2019-04-08T11:22:36"},

];

function compare(a, b) {
  const startA = new Date(a.startTime).getTime();
  const startB = new Date(b.startTime).getTime();
  return startA > startB ? -1 : 1;
}

console.log(array.sort(compare));

Comments

0

Aprreciate @mbojko's answer since User wants to sort array in desecnding order use startB - startA;

On sorting dates in decending order - most recent dates come first

var array = [
   {user:"Julia", startTime: "2019-04-09T11:22:36"}, 
   {user:"Charles", startTime:"2019-04-10T11:22:36"},
   {user:"Lisa", startTime:"2019-04-10T11:22:36"},
   {user:"Hank", startTime:"2019-04-11T11:22:36"},
   {user:"Hank", startTime:"2019-04-08T11:22:36"},

];

function compare(a, b) {

const startA = new Date(a.startTime).getTime();
const startB = new Date(b.startTime).getTime();
return startB - startA;
}

console.log(array.sort(compare));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.