I have dictionary Dictionary<string, Point>
我有字典
the Key is c1,c3,c2,t1,,t4,t2 I want to sort them to be c1,c2,c3,t1,t2,t3
关键是c1,c3,c2,t1, t4,t2我想把它们排序为c1,c2,c3,t1,t2,t3。
I'm trying to sort it using
我试着用排序
Input.OrderBy(key => key.Key );
but it doesn't work
但它不工作
any idea how to solve that
有办法解决这个问题吗
7 个解决方案
#1
-6
ok check this it should work
好的,检查一下,它应该工作。
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();
#2
25
Input.OrderBy
does not sort the dictionary, it creates a query that returns the items in an ordered order.
输入。OrderBy没有对字典排序,它创建了一个查询,该查询以有序的顺序返回这些项。
Perhaps OrderedDictionary gives you what you want.
也许是OrderedDictionary给你想要的东西。
Or use the Generic SortedDictionary
或者使用通用的SortedDictionary
#3
8
Load the unsorted object into a SortedDictionary object like so:
将未排序的对象加载到SortedDictionary对象中,如下所示:
SortedDictionary<string, string> sortedCustomerData = new SortedDictionary<string,string>(unsortedCustomerData);
Where unsortedCustomerData is the same generic type (Dictionary string, string or in your case string, point). It will automatically sort the new object by key
unsortedCustomerData是相同的泛型类型(字典字符串、字符串或大小写字符串、点)。它会自动按键对新对象进行排序。
According to msdn: SortedDictionary(IDictionary): Initializes a new instance of the SortedDictionary class that contains elements copied from the specified IDictionary and uses the default IComparer implementation for the key type.
根据msdn: SortedDictionary(IDictionary):初始化SortedDictionary类的一个新实例,该类包含从指定的IDictionary中复制的元素,并使用键类型的默认IComparer实现。
#4
5
Just a guess but it looks like you are assuming it is going to sort Input. The OrderBy method actually returns an ordered instance of an IOrderedEnumerable containing the same values. If you want to keep the return value you can do the below:
只是一个猜测,但看起来你假设它会对输入进行排序。OrderBy方法实际上返回包含相同值的IOrderedEnumerable的有序实例。如果你想保持回报值,你可以做如下:
IOrderedEnumerable orderedInput
orderedInput = Input.OrderBy(key=>key.Key)
Most methods that would modify the collection follow this same pattern. It does this so that it is not changing the origional collection instance. This protects you from accidently changing the instance when you didn't intend to. If you do want to only use the sorted instance then you just set the variable to the return of the method as shown above.
大多数修改集合的方法都遵循相同的模式。它这样做是为了不改变朝向集合实例。这可以保护您在无意中更改实例。如果您只想使用已排序的实例,那么您只需将该变量设置为如上所示的方法的返回值。
#5
3
Since Input.OrderBy creates a query that returns the items in an ordered order, just assign it to the same dictionary.
因为输入。OrderBy创建一个查询,该查询以有序的顺序返回项,只需将其分配给相同的字典。
objectDict = objectDict.OrderBy(obj => obj.Key).ToDictionary(obj => obj.Key, obj => obj.Value);
objectDict = objectDict。OrderBy(obj = > obj.Key)。ToDictionary(obj = > obj。键,obj = > obj.Value);
#6
2
The following code uses two more lists to sort a dictionary.
下面的代码使用另外两个列表对字典进行排序。
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);
}
}
}
The output of the code above is shown below.
上面代码的输出如下所示。
c1 1 2
c2 4 5
c3 0 1
c4 3 4
t1 5 6
t2 6 7
t3 2 3
#7
0
I used
我使用
var l = Input.OrderBy(key => key.Key);
and I converted it to Dictionary
我把它转换成了字典
#1
-6
ok check this it should work
好的,检查一下,它应该工作。
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();
#2
25
Input.OrderBy
does not sort the dictionary, it creates a query that returns the items in an ordered order.
输入。OrderBy没有对字典排序,它创建了一个查询,该查询以有序的顺序返回这些项。
Perhaps OrderedDictionary gives you what you want.
也许是OrderedDictionary给你想要的东西。
Or use the Generic SortedDictionary
或者使用通用的SortedDictionary
#3
8
Load the unsorted object into a SortedDictionary object like so:
将未排序的对象加载到SortedDictionary对象中,如下所示:
SortedDictionary<string, string> sortedCustomerData = new SortedDictionary<string,string>(unsortedCustomerData);
Where unsortedCustomerData is the same generic type (Dictionary string, string or in your case string, point). It will automatically sort the new object by key
unsortedCustomerData是相同的泛型类型(字典字符串、字符串或大小写字符串、点)。它会自动按键对新对象进行排序。
According to msdn: SortedDictionary(IDictionary): Initializes a new instance of the SortedDictionary class that contains elements copied from the specified IDictionary and uses the default IComparer implementation for the key type.
根据msdn: SortedDictionary(IDictionary):初始化SortedDictionary类的一个新实例,该类包含从指定的IDictionary中复制的元素,并使用键类型的默认IComparer实现。
#4
5
Just a guess but it looks like you are assuming it is going to sort Input. The OrderBy method actually returns an ordered instance of an IOrderedEnumerable containing the same values. If you want to keep the return value you can do the below:
只是一个猜测,但看起来你假设它会对输入进行排序。OrderBy方法实际上返回包含相同值的IOrderedEnumerable的有序实例。如果你想保持回报值,你可以做如下:
IOrderedEnumerable orderedInput
orderedInput = Input.OrderBy(key=>key.Key)
Most methods that would modify the collection follow this same pattern. It does this so that it is not changing the origional collection instance. This protects you from accidently changing the instance when you didn't intend to. If you do want to only use the sorted instance then you just set the variable to the return of the method as shown above.
大多数修改集合的方法都遵循相同的模式。它这样做是为了不改变朝向集合实例。这可以保护您在无意中更改实例。如果您只想使用已排序的实例,那么您只需将该变量设置为如上所示的方法的返回值。
#5
3
Since Input.OrderBy creates a query that returns the items in an ordered order, just assign it to the same dictionary.
因为输入。OrderBy创建一个查询,该查询以有序的顺序返回项,只需将其分配给相同的字典。
objectDict = objectDict.OrderBy(obj => obj.Key).ToDictionary(obj => obj.Key, obj => obj.Value);
objectDict = objectDict。OrderBy(obj = > obj.Key)。ToDictionary(obj = > obj。键,obj = > obj.Value);
#6
2
The following code uses two more lists to sort a dictionary.
下面的代码使用另外两个列表对字典进行排序。
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);
}
}
}
The output of the code above is shown below.
上面代码的输出如下所示。
c1 1 2
c2 4 5
c3 0 1
c4 3 4
t1 5 6
t2 6 7
t3 2 3
#7
0
I used
我使用
var l = Input.OrderBy(key => key.Key);
and I converted it to Dictionary
我把它转换成了字典