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
Abilash.SAbilash.S 

JSON Serialize and display List in descending order

Hi Everyone,
How to display JSON data by serializing in apex and display in descending order. Please have a look below json data.

let jsonData = [
{‘HouseName’:‘anna’,
‘Kilometers’:75
},
{‘HouseName’:‘nancy’,
‘Kilometers’: 55
},
{‘HouseName’:‘sana’,
‘Kilometers’:95
},
]

By getting this data from FE, parse it in apex, and provide list of housenames based on kilometers in descending order.

O/P-should be- [Sana,Anna,Nancy]
Please help me in Apex code.
mukesh guptamukesh gupta
Hi Abhi,

Please follow below code but it not test my side
 
public class MyClass
{
    public List<JSON2Apex> JSON2ApexItem{get;set;}   
    public MyClass()
    {
       let jsonData = [
						{"HouseName":"anna",
						"Kilometers":75
						},
						{"HouseName":"nancy",
						"Kilometers": 55
						},
						{"HouseName":"sana",
						"Kilometers":95
						}
						]
        JSON2ApexItem = (List<JSON2Apex>) System.JSON.deserialize(jsonData, List<JSON2Apex>.class);
        JSON2ApexItem.sort();
    }

public class JSON2Apex {

	public String HouseName;
	public Integer Kilometers;

	
	public static List<JSON2Apex> parse(String json) {
		return (List<JSON2Apex>) System.JSON.deserialize(json, List<JSON2Apex>.class);
	}
}
	
    
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh