Sunday, 17 June 2018

Sort Dictionary Based on Value in C#

As we already know that Dictionary works based on Key, Value pair. It provides us great flexibility to deal with lists or nested lists. Sorting a dictionary based on key is very easy but how would we sort it based on its value, I have designed a sample for a hint for you guys to help you with it.

Dictionary<string, int> Dict = new Dictionary<string, int>();
Dict.Add("James", 1);
Dict.Add("Lisa", 4);
Dict.Add("Ann", 2);
Dict.Add("Rick", 3);
var sortedDict = from x in Dict orderby x.Value ascending select x;

Output


You can also apply other options on other datatypes like StartsWith, Contains etc...

1 comment:

  1. Wynn Las Vegas and Encore Casino, Nevada - MapYRO
    The Wynn is a casino 광주 출장마사지 on the Las 상주 출장샵 Vegas Strip and is a 하남 출장안마 luxury resort with close proximity to Fashion Show Mall and 구미 출장안마 Sands 전라북도 출장샵 Expo Convention Center.Renovated in: 2002, 2005, 2010, 2011Construction started: 2001

    ReplyDelete