sagar43 sagar43 -4 years ago 190
C# Question

Parse Json string and sort based on property in C#

I am working on a window app in which a json string like


I want to parse and sort it by rating. Please suggest how i can achieve it.

Answer Source

You will need to deseralise the JSON into a Dictionary<int, YourClass>


var json = @"{

var result = JsonConvert.DeserializeObject<Dictionary<int,MyCustomClass>>(json);


public class MyCustomClass
    public Guid UID {get;set;}
    public decimal Rating {get;set;}

In the Dictionary<int, MyCustomClass> the int is the numeric key in your Json.

Note: I am using Newtonsoft Json here.

Once you have done the above you can sort it like the following using LINQ:

var sorted = result.OrderBy(x => x.Value.Rating);
// OR
var sorted = result.OrderByDescending(x => x.Value.Rating);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download