我有一个字典Dictionary<string, Point>
键是c1,c3,c2,t1,,t4,t2,我希望将它们排序为c1,c2,c3,t1,t2,t3
我正在尝试使用
Input.OrderBy(key => key.Key );
但它并不起作用。
有什么办法可以解决这个问题吗?
我有一个字典Dictionary<string, Point>
键是c1,c3,c2,t1,,t4,t2,我希望将它们排序为c1,c2,c3,t1,t2,t3
我正在尝试使用
Input.OrderBy(key => key.Key );
但它并不起作用。
有什么办法可以解决这个问题吗?
由于 Input.OrderBy 创建了一个按顺序返回项的查询,所以只需将其分配给同一个字典即可。
objectDict = objectDict.OrderBy(obj => obj.Key).ToDictionary(obj => obj.Key, obj => obj.Value);
像这样将未排序的对象加载到 SortedDictionary
对象中:
var sortedCustomerData
= new SortedDictionary<string, string>(unsortedCustomerData);
其中unsortedCustomerData
是相同泛型类型(Dictionary string, string
或在您的情况下为string, point
)。它会自动按键排序新对象。
根据 MSDN:SortedDictionary<TKey, TValue>(IDictionary<TKey, TValue>)
: 初始化一个 SortedDictionary<TKey, TValue>
类的新实例,该实例包含从指定的 IDictionary<TKey, TValue>
中复制的元素,并且对于键类型使用默认的 IComparer<T>
实现。
我猜你可能认为它将对输入进行排序。实际上,OrderBy方法返回一个有序的IOrderedEnumerable实例,其中包含相同的值。如果你想保留返回值,可以按照以下方式操作:
IOrderedEnumerable orderedInput
orderedInput = Input.OrderBy(key=>key.Key)
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
这取决于您的需求。如果您需要将键作为列表输出,一次排序即可解决问题。我已经编写了下面的测试代码,您可以运行并查看如何实现按键排序。
[Fact]
public void SortDict()
{
// Arrange
var initial = new Dictionary<string, bool>()
{
{"c1", true },
{"c3", true },
{"c2", true },
{"t1", true },
{"t3", true },
{"t2", true },
};
var expected = new List<string>() { "c1", "c2", "c3", "t1", "t2", "t3" };
// Act
var actual = initial.OrderBy(k => k.Key).Select(k => k.Key)
.ToList();
// Assert
actual.ShouldBeEquivalentTo(expected);
}
如果您需要始终按顺序排序键,则应使用SortedDictionary
。在下面的示例中,我正在使用其构造函数创建一个SortedDictionary
,并将旧字典作为参数传递。您可以运行测试并验证结果。
[Fact]
public void SortDictUsingLinq()
{
// Arrange
var initial = new Dictionary<string, bool>()
{
{"c1", true },
{"c3", true },
{"c2", true },
{"t1", true },
{"t3", true },
{"t2", true },
};
var expected = new List<string>() { "c1", "c2", "c3", "t1", "t2", "t3" };
// Act
var sortedDict = new SortedDictionary<string, bool>(initial);
// Assert
sortedDict.Keys.ToList().ShouldBeEquivalentTo(expected);
}
SortedDictionary
相比于Dictionary
具有更快的O(log n)插入和检索时间,而Dictionary
则具有O(1)。因此,如果您只需要对元素进行一次或很少排序,并且经常插入和删除,则一次排序就是您所需的。
我使用了
var l = Input.OrderBy(key => key.Key);
然后我将其转换为字典
好的,检查一下,应该可以工作。
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();