简体   繁体   中英

how to sort datas based on the key in dictionary in C#

I am using dictionary to collect some information. After I collect the details I need to sort the dictionary values based on the key using var Variable. After that I need to insert the sorted var variable to the new dictionary. Here, I have trouble to insert the sorted data into dictionary.

I'm posting my code Here:

var  orderDic = from dic in dictionaryColl
                orderby dic.Key  ascending
                select dic;             
Dictionary<int, string> newDic = new Dictionary<int, string>();
newDic =(Dictionary<int,string>)orderDic;  // Here i'm not able to assign data to dictionary

Thanks in Advance..

You can directly convert it to dictionary like:

Dictionary<int, string> newDic  = (from dic in dictionaryColl
                                  orderby dic.Key ascending
                                  select dic)
                                 .ToDictionary(r => r.Key, r => r.Value);

Or with method syntax:

Dictionary<int, string> newDic = dictionaryColl
                                .OrderBy(r => r.Key)
                                .ToDictionary(r => r.Key, r => r.Value);

EDIT: OrderBy would not effect the retrieval of Dictionary items in order.

See: Dictionary<TKey, TValue> Class

For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.

What you need is SortedDictionary like:

SortedDictionary<int, string> sortedNewDic = 
           new SortedDictionary<int, string>(dictionaryColl);

You just need

newDic = orderDic.ToDictionary(x=> x.Key, x => x.Value);

However this will not really help much as the order by clause won't affect the dictionary order.

It's possible you want a SortedDictionary ?

new Dic = new SortedDictionary(dictionaryColl);

为什么不首先使用SortedDictionary( http://msdn.microsoft.com/zh-cn/library/f7fta44c.aspx )?

newDic =(Dictionary<int,string>).OrderBy(l => l.ColName).ToList();

其中ColName是您要应用排序或排序的列的名称。

You can use

var _ordered = _dic.OrderBy(d => d.key).ToDictionary(d => d.Key, d => d.Value); 

method to order the dictionary in ascending order.

or

you can use

var _ordered = _dic.OrderByDescending(d => d.key).ToDictionary(d => d.Key, d => d.Value); 

method to order the dictionary in descending order.

Or else if you want to replace the same dictionary then

var _ordered = _dic.OrderByDescending(d => d.key).ToDictionary(d => d.Key, d => d.Value); 

in case your original Dictionary is _dic

_dic=new Dictionary<int, string>(_ordered);

Please go through the following code,

class Program
{
    static void Main(string[] args)
    {
        Dictionary<int,string> _dic=new Dictionary<int,string>();
        _dic.Add(5,"xyz");
        _dic.Add(2,"abc");
        _dic.Add(1,"pqr");
        _dic.Add(4,"test");
        _dic.Add(3,"pvf");

        var _ordered=_dic.OrderBy(d => d.Key).ToDictionary(d=> d.Key,d=>d.Value);
        _dic=new Dictionary<int,string>(_ordered);
    }
}

You can debug the above application and check that _dic is having the sorted dictionary based on the key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM