3

In my code I need sort the Array of JObject based on the date. how to do it? my data is like below I need to sort it into descending order.

"His": [
        {
        "Date": "6/17/2019 6:00:00 PM",
        "StatusMsg": ""
    },
    {
        "Date": "6/17/2019 11:08:48 AM",
        "StatusMsg": ""
    },
    {
        "Date": "6/17/2019 7:38:00 AM",
        "StatusMsg": ""
    },
    {
        "Date": "6/17/2019 5:00:00 AM",
        "StatusMsg": ""
    },
    {
        "Date": "6/16/2019 6:16:00 PM",
        "StatusMsg": ""
    },
    {
        "Date": "6/15/2019 9:20:00 PM",
        "StatusMsg": ""
    },
    {
        "Date": "6/13/2019 2:00:00 PM",
        "StatusMsg": ""
    },
    {
        "Date": "6/13/2019 1:37:00 PM",
        "StatusMsg": ""
    },
    {
        "Date": "6/13/2019 2:39:00 AM",
        "StatusMsg": ""
    },
    {
        "Date": "6/13/2019 2:11:00 AM",
        "StatusMsg": ""
    },
    {
        "Date": "6/13/2019 2:08:00 AM",
        "StatusMsg": ""
    },
    {
        "Date": "6/12/2019 3:50:00 PM",
        "StatusMsg": ""
    }
    ]

I assigned the result to JObject.This is my sample input. any one try to help me. thank you.

1
  • 3
    why not deserialize to a regular object model, where you can use all the usual tools? (in addition to being inconvenient to work with, a dynamic model like JObject has additional overheads, so it is also inefficient) Commented Jun 12, 2019 at 11:47

1 Answer 1

7

Like Marc said in the question comments - why not use a regular class:

public class Rootobject
{
    public His[] His { get; set; }
}

public class His
{
    public string Date { get; set; }
    public string StatusMsg { get; set; }
    public string Msg { get; set; }
    public string StatusDate { get; set; }
}

Deserialize it and take advantage of LINQ:

var json = "{\r\n\t\"His\": [\r\n        {\r\n            \"Date\": \"3/18/2019 6:30:45 PM\",\r\n            \"StatusMsg\": \"test3\"\r\n        },\r\n        {\r\n            \"Date\": \"3/21/2019 12:13:02 PM\",\r\n            \"Msg\": \"test2\"\r\n        },\r\n        {\r\n            \"StatusDate\": \"3/17/2019 9:26:00 AM\",\r\n            \"Msg\": \"test1\"\r\n        },\r\n    ]\r\n}";

var obj = JsonConvert.DeserializeObject<Rootobject>(json);

var sortedList = obj.His.OrderByDescending(o => o.Date).ToList();

Edit: If you don't want to use a class then you could do something like this:

var json = "{\r\n\t\"His\": [{\r\n\t\t\t\"Date\": \"3/18/2019 6:30:45 PM\",\r\n\t\t\t\"StatusMsg\": \"test3\"\r\n\t\t}, {\r\n\t\t\t\"Date\": \"3/21/2019 12:13:02 PM\",\r\n\t\t\t\"Msg\": \"test2\"\r\n\t\t}, {\r\n\t\t\t\"Date\": \"3/17/2019 9:26:00 AM\",\r\n\t\t\t\"Msg\": \"test1\"\r\n\t\t},\r\n\t]\r\n}\r\n";

JObject jobj = JObject.Parse(json);

JArray array = jobj["His"];

JArray sorted = new JArray(array.OrderByDescending(obj => (DateTime)obj["Date"]));

This assumes each JSON object will always have a property called "Date"

Sign up to request clarification or add additional context in comments.

8 Comments

Don't forget to use .OrderByDescending() instead of .OrderBy().
You dont have to convert to a List<T>.
Thanks for the response everyone. if i use a regular class i need to mention the fields in the class. But in this case the key value pair may be changed. but the fixed field is date. That is the reason i want to sort the object without regular class.
@SATHEESHP but in your example, your last object doesn't have "Date" but has "StatusDate"?
@chris it is not working. The output not in sorted order.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.