gpt4 book ai didi

C# - Dictionary> 排序抛出 System.ArgumentException

转载 作者:太空宇宙 更新时间:2023-11-03 21:54:53 26 4
gpt4 key购买 nike

我有一个未排序的 Dictionary<int, Dictionary<int, string>>并在尝试对其值的键进行排序时抛出 System.ArgumentException: At least one object must implement IComparable.以下是函数,

private static Dictionary<int, Dictionary<int, string>> SortDictionary(Dictionary<int, Dictionary<int, string>> unSortedDict)
{
var sortedDict = (unSortedDict.OrderBy(entry => entry.Value.Keys)).ToDictionary(pair => pair.Key, pair => pair.Value);

return sortedDict;
}

我附上了下面的数据屏幕截图,您可以在其中看到未排序的键,这样您就知道我要实现的目标,我做错了什么,我需要您的指导,谢谢

enter image description here

最佳答案

根据设计,字典本质上是未排序的。如果您对字典值进行排序,然后将其转换回字典,您将再次得到一个未排序的集合。

也就是说,发生错误是因为您正在尝试调用 OrderBy KeyCollection ,这不是可排序的类型。

您可能需要考虑一些其他集合,例如 SortedDictionary<T,U> .但是,这将需要编写自定义比较器来维护排序。

关于C# - Dictionary<int, Dictionary<int, string>> 排序抛出 System.ArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12732120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com