简体   繁体   English

排序列表项

[英]Sorted list items

Is it possible to add items to sorted list with the same value, because when I am trying to do so it shows me an error: 是否可以将项目添加到具有相同值的排序列表中,因为当我尝试这样做时它会显示错误:
"An entry with the same key already exists." “已存在具有相同密钥的条目。”
If it's possible, then how? 如果有可能,那怎么样?

It is not possible* to add duplicate keys as stated by other users. 如其他用户所述,添加重复密钥是不可能的。

In c# you might be able to use the Lookup class instead, which allows multiple values to be stored with the same key. 在c#中,您可以使用Lookup类,它允许使用相同的键存储多个值。

See: http://msdn.microsoft.com/en-us/library/bb460184.aspx 请参阅: http//msdn.microsoft.com/en-us/library/bb460184.aspx

* It is possible, see comments, but only by defining a comparitor that never returns equality for equal items, which IMO is a really really bad idea. *有可能,请参阅注释,但只能通过定义一个永远不会返回相等项目的相等性的比较器,IMO真的是一个非常糟糕的主意。

No, it's not possible. 不,这是不可能的。 The keys must be unique. 密钥必须是唯一的。

Define a class that implements IComparer . 定义一个实现IComparer的类。 When you instantiate the SortedList , you pass in an instance of your class. 实例化SortedList ,将传入类的实例。 Check out Knasterbax's answer . 查看Knasterbax的答案

Key should be unique. 密钥应该是唯一的。 See this in MSDN 在MSDN中看到这一点

ArgumentException   - An element with the specified key already exists in the SortedList object.

http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.add.aspx http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.add.aspx

暂无
暂无

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

相关问题 构造具有优先级项的排序列表 - Constructing a Sorted List with priority items 项目以错误的顺序添加到排序列表中 - Items being added into sorted list in wrong order 如何在排序列表中查找所有项目 <T> 使用.NET 2.0? - How to find all items in a sorted List<T> using .NET 2.0? 下拉列表,其中根据第二个变量(按第三个变量排序)删除了项 - Dropdown list with items removed based on second variable sorted by third variable 将项目添加到已排序的 observablecollection - Adding items to a sorted observablecollection 从列表中选择一个新的排序列表,我只需要x个项目,并且在获取顶级项目之前不想对整个列表进行排序 - geting a new sorted list from a list, i only need x items and dont want to sort the whole list before getting the top items 排序对象列表…需要按字母顺序排序的属性,除非需要列表中带有特定字母FIRST的项目 - Sorting a list of objects… need property to be sorted alphabetically EXCEPT need items with a specific letter FIRST in list 验证列表已排序且项目具有特殊字符时获取 NUnit AssertionException - Getting NUnit AssertionException when verifying list is sorted and items have special characters 如何通过关系子/父的 2 个字段排序。 对其进行排序以使子项在排序列表中跟随其父项? - How to order by 2 fields with relation child/parent. Sort it so that child items follow their parents in the sorted list? CollectionViewSource with Sort get sorted Items - CollectionViewSource with Sort get sorted Items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM