2011-11-13 59 views
22

我有字典Dictionary<string, Point>如何排序的字典键

关键是C1,C3,C2,T1,T4,T2我想对它们进行排序为C1,C2,C3,T1,T2, T3

我试图使用

Input.OrderBy(key => key.Key); 

进行排序,但它不工作

任何想法如何解决

+1

它没有排序,或者只是不按照您期望的顺序? –

+0

不 – AMH

+1

http://stackoverflow.com/questions/289/how-do-you-sort-a-c-sharp-dictionary-by-value 引用排序,这样它有一些很好的例子。 –

回答

-5

确定检查这一点,应该工作

var r = new Dictionary<string, Point>(); 
r.Add("c3", new Point(0, 0)); 
r.Add("c1", new Point(0, 0)); 
r.Add("t3", new Point(0, 0)); 
r.Add("c4", new Point(0, 0)); 
r.Add("c2", new Point(0, 0)); 
r.Add("t1", new Point(0, 0)); 
r.Add("t2", new Point(0, 0)); 
var l = r.OrderBy(key => key.Key); 
var dic = l.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value); 

foreach (var item in dic) 
{ 

    Console.WriteLine(item.Key); 
} 
Console.ReadLine(); 
+1

-1:这不会工作。您只是创建了另一个没有排序的字典。它可能适用于小型字典,但最终会失败。 – leppie

+0

我们在聊天时发了脾气,我知道他想要什么,所以我帮助他解决了他的问题 – DeveloperX

+10

@DeveloperX - 我的猜测是人们投票不起作用,因为它没有回答问题。我们没有看到聊天,所以人们可能不认为这是我们看到的问题的有用答案。 –

5

只是猜测,但它看起来像你正在假设它将排序输入。 OrderBy方法实际上会返回包含相同值的IOrderedEnumerable的有序实例。如果你想保留的返回值,你可以做以下:

IOrderedEnumerable orderedInput 
orderedInput = Input.OrderBy(key=>key.Key) 

,将修改集合大多数方法遵循相同的模式。它这样做是为了不改变原始收藏实例。这样可以防止您在无意的情况下意外更改实例。如果您只想使用已排序的实例,则只需将该变量设置为方法返回,如上所示。

25

Input.OrderBy不对字典进行排序,它会创建一个按有序顺序返回项目的查询。

也许OrderedDictionary给你你想要的。

或者使用通用SortedDictionary

+0

但它不是通用的,它的性能将会是问题 – AMH

+2

@AMH不要对性能做出假设,直到你真实地测试它(微 - 基准不计算)。 – Richard

+2

你怎么知道性能会成为问题? –

0

我用

var l = Input.OrderBy(key => key.Key); 

,我把它转换到字典

+1

A我说过:这不会对字典进行排序!它创建一个排序的“视图”。 –

4

装入未分类的对象为像这样的SortedDictionary对象:

SortedDictionary<string, string> sortedCustomerData = new SortedDictionary<string,string>(unsortedCustomerData); 

其中unsortedCustomerData是相同的泛型类型(字符串,字符串或在您的案例字符串,点)。它会自动按键排序新对象

根据msdn:SortedDictionary(IDictionary):初始化SortedDictionary类的新实例,该实例包含从指定的IDictionary复制的元素,并将默认的IComparer实现用于键类型。

2

以下代码使用另外两个list s到sort一个字典。

using System; 
using System.Collections.Generic; 
using System.Drawing; 

namespace ConsoleApplication1 { 
    class Program { 
     static void Main(string[] args) { 
      Dictionary<string,Point> r=new Dictionary<string,Point>(); 
      r.Add("c3",new Point(0,1)); 
      r.Add("c1",new Point(1,2)); 
      r.Add("t3",new Point(2,3)); 
      r.Add("c4",new Point(3,4)); 
      r.Add("c2",new Point(4,5)); 
      r.Add("t1",new Point(5,6)); 
      r.Add("t2",new Point(6,7)); 
      // Create a list of keys 
      List<string> zlk=new List<string>(r.Keys); 
      // and then sort it. 
      zlk.Sort(); 
      List<Point> zlv=new List<Point>(); 
      // Readd with the order. 
      foreach(var item in zlk) { 
       zlv.Add(r[item]); 
      } 
      r.Clear(); 
      for(int i=0;i<zlk.Count;i++) { 
       r[zlk[i]]=zlv[i]; 
      } 
      // test output 
      foreach(var item in r.Keys) { 
       Console.WriteLine(item+" "+r[item].X+" "+r[item].Y); 
      } 
      Console.ReadKey(true); 
     } 
    } 
} 

以上代码的输出如下所示。

c1 1 2 
c2 4 5 
c3 0 1 
c4 3 4 
t1 5 6 
t2 6 7 
t3 2 3 
2

由于Input.OrderBy创建一个按有序顺序返回项目的查询,只需将其分配给同一个字典。

objectDict = objectDict.OrderBy(obj => obj.Key).ToDictionary(obj => obj.Key, obj => obj.Value);