Sorting JSON array by date

Please help me to sort the Json array based the date. Please see below input data. The out put should come the latest date item should be on top. I have tried this one but its throwing the exception

eventsJsarray.Cast(Of JObject).Where(Function (x) x(“event”).toString).OrderByDescending(function(x) Cdate(x(“loggedDate”).ToString)).FirstOrDefault

"event": [
        {
            "agnNumber": "123456",
			"agnName": "Mathew",
			"City": "Chennai",
			"loggedDate": "2025-01-26 09:45:49.926",
           
        },
		   {
            "agnNumber": "123768",
			"agnName": "enraled",
			"City": "hyd",
			"loggedDate": "2025-01-25 09:45:49.926",
           
        },
		   {
            "agnNumber": "123458",
			"agnName": "Mathew",
			"City": "bnglr",
			"loggedDate": "2024-01-24 09:45:49.926",
           
        },
		   {
            "agnNumber": "123458",
			"agnName": "Mathew",
			"City": "mubai",
			"loggedDate": "2025-01-27 09:45:49.926",
           
        }
]

@thotlamahesh_kumar ,

Not tested but can you try this one

' Assume you have a JSON string stored in a variable named jsonString
Dim jsonArray As JArray = JArray.Parse(jsonString)
Dim sortedArray As JArray = JArray.FromObject(jsonArray.OrderByDescending(Function(x) DateTime.Parse(x("loggedDate").ToString())).ToList())

@thotlamahesh_kumar

this is how you would sort

jarr_events.OrderBy(function(x) DateTime.ParseExact(x("loggedDate").ToString,"yyyy-MM-dd HH:mm:ss.fff",System.Globalization.CultureInfo.InvariantCulture)).FirstOrDefault

input json

cheers

Thanks @ashokkarale for your reply
let me try and i will provide the update

1 Like

Thanks a lot @Anil_G , its worked but results came in reverse order
the old date came first and new date came last

1 Like

@thotlamahesh_kumar

Then use .Last

Or use OrderByDescending instead of OrderBy

Hope this helps

Cheers