0

I am deserializing a JSON string into a dictionary in C# however I would like to sort the dictionary by a value in order to display it on a table of high scores. Right now the Dictionary returns in fixed order.

  var temp = Json.Deserialize(www.text) as Dictionary<string,object>;
            if (temp != null)
            {
                data = (List<object>)temp["_items"];
            }

JSON String

   "_items":[  
      {  
         "_updated":"Thu, 01 Jan 1970 00:00:00 GMT",
         "_id":"-",
         "Name":"John Doe",
         "_links":{  
            "self":{  
               "href":"Players/-",
               "title":"Player"
            }
         },
         "_created":"Thu, 01 Jan 1970 00:00:00 GMT",
         "FacebookId":XXXXX,
         "HighScore":8862,
         "_etag":"-"
      },
      {  
         "_updated":"Thu, 01 Jan 1970 00:00:00 GMT",
         "_id":"-",
         "Name":"John Smith",
         "_links":{  
            "self":{  
               "href":"Players/-",
               "title":"Player"
            }
         },
         "_created":"Thu, 01 Jan 1970 00:00:00 GMT",
         "FacebookId":XXXXXX,
         "HighScore":32000,
         "_etag":"-"
      }
   ],

Cell Creation

var dict = (Dictionary<string, object>) data[row];


            // Set Name
            cell.nameText.text = dict["Name"].ToString();

            // Set score
            cell.score.text = string.Format("{0:n0}", dict["HighScore"]);

            // Fb Profile IMG
            StartCoroutine(getFBPicture(cell.image, dict["FacebookId"].ToString()));
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Prad
  • 469
  • 1
  • 8
  • 28
  • I recommend using LINQ: https://stackoverflow.com/questions/5949904/c-sharp-sort-dictionary-with-linq – MessLuke Jun 02 '17 at 21:27
  • Possible duplicate of [How do you sort a dictionary by value?](https://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value) – FortyTwo Jun 02 '17 at 21:28
  • @ISHIDA by the HighScore Variable shown in the JSON – Prad Jun 02 '17 at 21:30
  • 1
    "Right now the Dictionary returns in fixed order" - well, not in a well-defined, documented order. You should regard `Dictionary<,>` as an unordered collection. Any particular ordering you see now is *not* guaranteed to be the order you see in another version, for example. (And adding or removing an item could change the entire order.) – Jon Skeet Jun 02 '17 at 21:52
  • It also seems to me that instead of a `Dictionary` you'd be wise to create a type (e.g. `Player`) to deserialize each value to... – Jon Skeet Jun 02 '17 at 21:53

2 Answers2

0

Try this.

public class obj 
                {
                public DateTime updated {get;set;}
                public string id {get;set;}
                public string Name {get;set;}
                 public self links {get;set;}
                public string FacebookId {get;set;}
                public int highscore {get;set;}

                }

    public class self {
    public string href {get;set;}
    public string title {get;set;}
                   }

                var dic = new Dictionary<TKey, obj>();
                var sorted = dic.OrderBy(x=>x.Value.highscore).ToList();
ISHIDA
  • 4,700
  • 2
  • 16
  • 30
0

As you are using a Dictionary, it is not possible to sort the dictionary itself, as the purpose of the dictionary is to access it using a Hash value. However, you can return a sorted value list by doing something like this:

dictionary.Values.OrderyBy(x => (DateTime)x._updated);

this is assuming you are using something like Newtonsoft.Json that returns an object with accesible properties.

Please consider that you should use an object of a basic type or a type that implements IComparable as the one to define the sort order.

rovinos
  • 222
  • 1
  • 3
  • 7