function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
krishna casukhelakrishna casukhela 

sort array of json values coming from api call

I have a json which comes from api as follows

{"medicaidDualStatuesResponse": {"readMedicaidDualStatusesResponse": {"medicareMember": [
      {
      "medicaidDualStatus": [      {
         "dualStatusIndicator": "false",
         "source": "MedicaidDualStatusSourceCode",
         "code": "01",
         "codeName": "MedicaidDualStatusCode",
         "effectivePeriod":          {
            "datetimeBegin": "2018-03-01T00:00:00-05:00",
            "datetimeEnd": "2018-07-31T00:00:00-04:00"
         }
      }],
      "medicareIdentifier":       {
         "idSource": "MBI",
         "idValue": "9Y12M55CD02"
      }
   },
      {
      "medicaidDualStatus":       [
                  {
            "dualStatusIndicator": "false",
            "source": "MedicaidDualStatusSourceCode",
            "code": "01",
            "codeName": "MedicaidDualStatusCode",
            "effectivePeriod":             {
               "datetimeBegin": "2018-10-01T00:00:00-04:00",
               "datetimeEnd": "2018-11-30T00:00:00-05:00"
            }
         },
                  {
            "dualStatusIndicator": "false",
            "source": "MedicaidDualStatusSourceCode",
            "code": "01",
            "codeName": "MedicaidDualStatusCode",
            "effectivePeriod":             {
               "datetimeBegin": "2019-01-01T00:00:00-05:00",
               "datetimeEnd": "2019-03-31T00:00:00-04:00"
            }
         }
      ],
      "medicareIdentifier":       {
         "idSource": "MBI",
         "idValue": "9Y12M55CD04"
      }
   },
      {
      "medicaidDualStatus": [],
      "medicareIdentifier":       {
         "idSource": "HICN",
         "idValue": ""
      }
   }
]}}}
 
so in the controller of my lightning component I did as follows
 var medicareMemberList = QMBResponse.medicaidDualStatuesResponse.readMedicaidDualStatusesResponse.medicareMember;

Please let me know how to sort the dates given in the json in descending order. 

I am new to this so do not have much knowledge.
thanks

 
Bhanu Tanaka 5Bhanu Tanaka 5
Hi Krishna-

Just had to solve a similar problem.  Created a Class to store my JSON objects that needed to be sorted.  Implemented the Comparable interface and reversed my results in the compareTo() method.  Therefore all calls to .sort() were in descending order.  Did the work in my APEX controller - not sure if there's an easier way to do this in JS - hope this helps!

JSON Class
global class ccv_Activity implements Comparable {

    global String status;
    global String type;
    global String activityDate;
    global String ownerFullName;
    global String name;
    global String description;
    global Date activityDateTypedDate;

    global ccv_Activity() {

    }
    // implementation of compareTo
    // NOTE: sort() should always return this as Descending which is why
    //       return values are switched
    global integer compareTo ( Object compareToObj ) {
        // cast
        ccv_Activity compareToActivity = (ccv_Activity) compareToObj;
        // compare the dates
        if ( this.activityDateTypedDate == compareToActivity.activityDateTypedDate ) {
            return 0;
        } else if ( this.activityDateTypedDate > compareToActivity.activityDateTypedDate ) {
            return -1;
        } else if ( this.activityDateTypedDate < compareToActivity.activityDateTypedDate ) {
            return 1;
        }
    }
}

Resulting call to sort by Date descending
practice.activities.sort ();

Note that activities above is of type ccv_Activity