1

I am working on one small application
in that i want to sort the array of objets by two values
one is time stamp and one in string

I tried this below snippet but its not satisfying the below condition

I want this object mentioned in snippet should sorted based on the object should contain sport as cricket first and empty value as second and both are sorted by date ascending order

var content =  [
        {
            "first_name": "musk",
            "sport": "cricket",
            "created_date": "2022-08-12 04:03:08",            
        },
        {
            "first_name": "john",
            "sport": "",
            "created_date": "2022-08-01 23:00:46",
            
        },
         {
            "first_name": "robot",
            "sport": "cricket",
            "created_date": "2022-08-10 23:00:46",
            
        },
        {
            "first_name": "roy",
            "sport": "",
            "created_date": "2022-07-31 23:00:46",
            
        },
]


    content.sort(function compare( a, b ) {
            if ( a.sport < b.sport || (new Date(a.created_date) < new Date(b.created_date))){
                return 1;
            }
            if ( a.sport > b.sport || (new Date(a.created_date) < new Date(b.created_date)) ){
                return -1;
            }
            return 0;
        });
        
console.log(content)

Attached the expected result

expected result

var content =  [
         {
            "first_name": "robot",
            "sport": "cricket",
            "created_date": "2022-08-10 23:00:46",
            
        },
        
          {
            "first_name": "musk",
            "sport": "cricket",
            "created_date": "2022-08-12 04:03:08",            
        },
        {
            "first_name": "roy",
            "sport": "",
            "created_date": "2022-07-31 23:00:46",
            
        },
        {
            "first_name": "john",
            "sport": "",
            "created_date": "2022-08-01 23:00:46",
            
        },
       
]

can anyone help me with what i am doing wrong in snippet

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Dinesh s
  • 313
  • 4
  • 19
  • 1
    Does this answer your question? [How to sort an array of objects by multiple fields?](https://stackoverflow.com/questions/6913512/how-to-sort-an-array-of-objects-by-multiple-fields) – Heretic Monkey Aug 12 '22 at 11:06

2 Answers2

1

That should do the job:

var content =  [
      {
          "first_name": "musk",
          "sport": "cricket",
          "created_date": "2022-08-12 04:03:08",            
      },
      {
          "first_name": "john",
          "sport": "",
          "created_date": "2022-08-01 23:00:46",

      },
      {
          "first_name": "robot",
          "sport": "cricket",
          "created_date": "2022-08-10 23:00:46",

      },
      {
          "first_name": "roy",
          "sport": "",
          "created_date": "2022-07-31 23:00:46",

      },
];

content.sort(function compare(a, b) {
      // compare the "sport" fields
      if(a.sport > b.sport){
        return -1;
      }
      if(a.sport < b.sport){
        return 1;
      }
      // compare the dates
      if(new Date(a.created_date) < new Date(b.created_date)){
        return -1;
      }
      if(new Date(a.created_date) > new Date(b.created_date)){
        return 1;
      }
      return 0;
});

console.log(content);

content.sort(function compare(a, b) {
      // compare the "sport" fields based on they include "cricket" string or not
       if(a.sport.includes("cricket") && !b.sport.includes("cricket")){
        return -1;
      }
      if(!a.sport.includes("cricket") && b.sport.includes("cricket")){
        return 1;
      }
      // compare the dates
      if(new Date(a.created_date) < new Date(b.created_date)){
        return -1;
      }
      if(new Date(a.created_date) > new Date(b.created_date)){
        return 1;
      }
      return 0;
});

console.log(content);
mikyll98
  • 1,195
  • 3
  • 8
  • 29
  • Careful, you're comparing only the length of `sport` – CcmU Aug 12 '22 at 10:28
  • @CcmU isn't that what's he asking in the question? He's asking to compare sport field based on if it contains "cricket" or not, and that's one way to achieve it. It could aswell just check if it contains "cricket" or not, using .include("cricket") – mikyll98 Aug 12 '22 at 10:30
  • Thanks @mikyll98 , i will add a check for sport cricket as well – Dinesh s Aug 12 '22 at 10:32
  • @mikyll98 actually it's not clear if we should only consider `cricket` as a possible value for the question per se. For the given example it could be enough, but it will most certainly not work for a more general use. – CcmU Aug 12 '22 at 10:37
  • @mikyll98 if we want to add check as well we want to add check for both a and b right or instead of length how to sort by only adding the check – Dinesh s Aug 12 '22 at 11:38
  • @Dineshs could you reformulate the question? I didn't quite understand. What would you like to achieve exactly? You want to compare based on if they contain "cricket" or not, and then compare by date? – mikyll98 Aug 12 '22 at 11:47
  • @mikyll98 instead of length check i need to check value contains cricket how to add the same in condition – Dinesh s Aug 12 '22 at 11:52
  • @Dineshs I've updated the answer with a second sort, which uses the compare you're asking, let me know if that answers your question :) – mikyll98 Aug 12 '22 at 11:59
0

Check for date comparison only when sport values are equal, like this:

var content =  [
        {
            "first_name": "musk",
            "sport": "cricket",
            "created_date": "2022-08-12 04:03:08",            
        },
        {
            "first_name": "john",
            "sport": "",
            "created_date": "2022-08-01 23:00:46",
            
        },
         {
            "first_name": "robot",
            "sport": "cricket",
            "created_date": "2022-08-10 23:00:46",
            
        },
        {
            "first_name": "roy",
            "sport": "",
            "created_date": "2022-07-31 23:00:46",
            
        },
]


    content.sort(function compare( a, b ) {
            if(a.sport === b.sport) {
               return (new Date(a.created_date) < new Date(b.created_date)) ? -1 : new Date(a.created_date) === new Date(b.created_date) ? 0 : 1
            }
            return a.sport < b.sport ? 1: -1
        });
        
console.log(content)
Charchit Kapoor
  • 8,934
  • 2
  • 8
  • 24