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

1

2 Answers 2

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

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

8 Comments

Careful, you're comparing only the length of sport
@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")
Thanks @mikyll98 , i will add a check for sport cricket as well
@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.
@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
|
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)

2 Comments

did you test it before writing the answer? a.sport === b.soprt)
Thanks, @skalpel02 made a typo while editing the answer.

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.