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
Mounika@9999Mounika@9999 

How to sort below JSON response with latest order date? please give me apex code for this..

Input:
-------
{
    "Rows": [                                                       
        [22847938, "SO", "202303161732488-MG", 1, 0, 0, 0, 1, 2.5, "2023-03-16T00:00:00", "2024-03-16T00:00:00", "OPEN", null, null, "2023-03-16T00:00:00", "009", "PUCG", "N30", true, false, "9800 De Soto Ave. - 009", null, null, "Chatsworth", "CA", "91311", "US", null, 0.0, null, null, 154023, []],
        [22878665, "SO", "202303231535745", 16, 0, 0, 0, 16, 128.25, "2023-03-23T00:00:00", "2024-03-23T00:00:00", "OPEN", null, null, "2023-03-23T00:00:00", "010", "UPSG", "N30", true, false, "13100 Tonopah ST", null, null, "Chatsworth", "CA", "91311-____", "US", null, 0.0, null, null, 178878, []],
        [22878689, "SO", "202303231539239", 6, 0, 0, 0, 6, 19.4, "2023-03-31T00:00:00", "2024-03-23T00:00:00", "OPEN", null, null, "2023-03-31T00:00:00", "010", "UPSG", "N30", true, false, "13100 Tonopah ST", null, null, "Chatsworth", "CA", "91311-____", "US", null, 0.0, null, null, 178902, []]
    ]
}
Expected Output
---------------

{
    "Rows": [                                                       
        [22878689, "SO", "202303231539239", 6, 0, 0, 0, 6, 19.4, "2023-03-31T00:00:00", "2024-03-23T00:00:00", "OPEN", null, null, "2023-03-31T00:00:00", "010", "UPSG", "N30", true, false, "13100 Tonopah ST", null, null, "Chatsworth", "CA", "91311-____", "US", null, 0.0, null, null, 178902, []],
        [22878665, "SO", "202303231535745", 16, 0, 0, 0, 16, 128.25, "2023-03-23T00:00:00", "2024-03-23T00:00:00", "OPEN", null, null, "2023-03-23T00:00:00", "010", "UPSG", "N30", true, false, "13100 Tonopah ST", null, null, "Chatsworth", "CA", "91311-____", "US", null, 0.0, null, null, 178878, []],
        [22847938, "SO", "202303161732488-MG", 1, 0, 0, 0, 1, 2.5, "2023-03-16T00:00:00", "2024-03-16T00:00:00", "OPEN", null, null, "2023-03-16T00:00:00", "009", "PUCG", "N30", true, false, "9800 De Soto Ave. - 009", null, null, "Chatsworth", "CA", "91311", "US", null, 0.0, null, null, 154023, []]
    ]
}
Mukesh pal 7Mukesh pal 7

check this snippet hope this helps you !!

// Parse the JSON response into an Apex object
String jsonString = '[{"name": "Product A", "order_date": "2022-03-01"}, {"name": "Product B", "order_date": "2022-02-15"}, {"name": "Product C", "order_date": "2022-03-10"}]';
List<Object> jsonList = (List<Object>) JSON.deserializeUntyped(jsonString);

// Extract the relevant data and sort by order date
List<Map<String, Object>> productList = new List<Map<String, Object>>();
for (Object obj : jsonList) {
    Map<String, Object> productMap = (Map<String, Object>) obj;
    productList.add(productMap);
}
productList.sort((a, b) => ((String) a.get('order_date')).compareTo((String) b.get('order_date')));

// Serialize the sorted list back to JSON
String sortedJsonString = JSON.serialize(productList);
System.debug('Sorted JSON: ' + sortedJsonString);

 

we first parse the JSON response into an untyped Apex object using the JSON.deserializeUntyped method. Then, we extract the product data from the object and add it to a new list called productList. Finally, we sort the productList based on the order_date field using a lambda expression, and serialize it back to a JSON string using the JSON.serialize method.
Note that in the example above, we assume that the order_date field is a string in the format 'YYYY-MM-DD'. If the date format is different, you may need to modify the code accordingly.

 

Mounika@9999Mounika@9999
Hi , 
Thanks for ur message.
Already i tried that code, it is showing so amny syntactical errors while deploying the code.