简体   繁体   English

如何基于C#中字典中的键对数据进行排序

[英]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. 收集详细信息后,需要使用var Variable根据键对字典值进行排序。 After that I need to insert the sorted var variable to the new dictionary. 之后,我需要将排序后的var变量插入到新字典中。 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. 编辑: OrderBy不会影响字典中订单项的检索。

See: Dictionary<TKey, TValue> Class 请参阅: 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. 出于枚举目的,字典中的每个项目都被视为代表值及其键的KeyValuePair结构。 The order in which the items are returned is undefined. 返回项目顺序是不确定的。

What you need is SortedDictionary like: 您需要的是SortedDictionary,例如:

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. 但是,这实际上并没有多大帮助,因为order by子句不会影响字典的顺序。

It's possible you want a SortedDictionary ? 您可能需要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

_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. 您可以调试上面的应用程序,并检查_dic是否具有基于键的排序字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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