CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using NUnit.Framework; // Throws NUnit.Framework.AssertionException
//https://stackoverflow.com/questions/53326681/sorting-jobject-inside-array-by-a-field-value
class TestClass
{
public static void Test()
{
var data = GetJson();
// Load the JSON without parsing or converting any dates.
var model = JsonConvert.DeserializeObject<JObject>(data, new JsonSerializerSettings{ DateParseHandling = DateParseHandling.None });
Console.WriteLine("Input model: ");
Console.WriteLine(model);
// Construct a serializer that converts all DateTime values to UTC
var serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings{ DateTimeZoneHandling = DateTimeZoneHandling.Utc });
foreach (var locations in model.SelectTokens("Result[*].locations").OfType<JArray>())
{
// Then sort the locations by utcDate converting the value to UTC at this time.
var query = from location in locations
let utcDate = location.SelectToken("locTypeAttributes.utcDate").ToObject<DateTime>(serializer)
orderby utcDate
select location;
locations.ReplaceAll(query.ToList());
}
Console.WriteLine("\nSorted model: ");
Console.WriteLine(model);
}
static string GetJson()
{
var json = @"{
""MetaData"": {
""ResourcesUsed"": 1
},
""Result"": [
{
""locations"": [
{
""country"": ""Papua New Guinea"",
""city"": ""Jacquinot Bay"",
""locTypeAttributes"": {
""localDate"": ""2018-10-08T04:21:00-07:00"",
""utcDate"": ""2018-10-08T04:21:00-07:00"",
},
""point"": {
""coordinates"": [
151.52,
-5.6
],
""type"": ""Point""
}
},{
""country"": ""Papua New Guinea2"",
""city"": ""Jacquinot Bay2"",
""locTypeAttributes"": {
""localDate"": ""2018-10-08T04:21:00-07:00"",
""utcDate"": ""2018-10-02T04:21:00-07:00"",
},
""point"": {
""coordinates"": [
151.52,
-5.6
],
""type"": ""Point""
}
}
]
}
]
}
";
return json;
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
}
#if false
Do you want to sort the `"Result": []` array, or do you want to sort each `"Result[*].locations"` array? Because the locations themselves are an array so there might in principle be more than one, each with its own `utcDate`. And if you do want to sort the `"Results"` array how do you want to deal with the situation where a result has multiple locations with multiple `utcDate` values? [C# Sort JSON string keys](https://stackoverflow.com/q/14417235/3744182) is for recursively sorting a tree of objects by property name so it doesn't really apply here.
#endif