I am using System.Text.Json and right now, I'm trying to figure out how to sort a JsonArray without deserializing it, if that is at all possible. I have been able go get other LINQ functions to work with JsonArrays, however, I cannot seem to get OrderBy to work and I am getting compile errors with everything I have tried.
When I set a var to be the result of the sorted array, VS says that it is:
IOrderedEnumerable<JsonNode>
VS will not allow me to convert this type to a JsonArray. If I use JsonArray instead of var, then I get the red squiggly lines and the error says:
CS0029: Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<System.Text.Json.Nodes.JsonNode>' to System.Text.Json.Nodes.JsonArray
I have been looking all around and I have not had much luck when it comes to sorting JsonArrays. Is this even possible?
Below are some of the things I have tried:
private void SortArrayTest(JsonArray arrayToSort)
{
var sortedArray0 = arrayToSort.OrderBy(x => x["Name"].ToString());
JsonArray sortedArray1 = arrayToSort.OrderBy(x => x["Name"]);
JsonArray sortedArray2 = arrayToSort.OrderBy(x => x["Name"].ToString());
JsonArray sortedArray3 = arrayToSort.OrderBy(x => x["Name"].ToString()).ToArray();
JsonArray sortedArray4 = arrayToSort.OrderBy(x => x["Name"].ToString()).ToList();
JsonArray sortedArray5 = arrayToSort.OrderBy(x => x["Name"].ToString()).ToList<string>();
JsonArray sortedArray6 = arrayToSort.OrderBy(x => (string)x["Name"]).ToArray();
}