我有一组元素/键,我正在从两个不同的配置文件中读取它们。因此,键可能相同,但每个键具有不同的关联值。
我想按排序顺序列出它们。我能做些什么 ?我尝试使用 SortedList
类,但它不允许重复的键。
我该怎么做呢?
例如,假设我有 3 个元素,键分别为 1、2、3。然后我又得到一个具有键 2 (但值不同)的元素。然后我希望将新密钥插入到现有密钥 2 之后但在 3 之前。如果我再次找到具有密钥 2 的元素,那么它应该位于最近添加的密钥 2 之后。
请注意,我使用的是 .NET 2.0
I have a set of elements/keys which I'm reading from two different config files. So the keys may be same but with different values associated with each of them.
I want to list them in the sorted order. What can I do ? I tried with SortedList
class but it does not allow duplicate keys.
How can I do it?
e.g Lets say I have 3 elements with keys 1,2,3. Then i get one more element having key 2 (but different value). Then I want the new key to get inserted after existing key 2 but before 3. If I againg find an element with key 2, then it should go after most recently added key 2.
Please note than I'm using .NET 2.0
发布评论
评论(10)
我更喜欢使用 LINQ 来完成此类事情:
注意:您必须使用 .NET 3.5 或更高版本才能完成此任务。
I prefer to use LINQ for this type of thing:
Note: you must be using .NET 3.5 or later to accomplish this.
您需要的是带有自定义 IComparer 的排序函数。现在您拥有的是使用排序时的默认 icomparer。这将检查字段值。
当您创建自定义 IComparer 时(您可以通过实现 Icomparable 在类中执行此操作 接口)。它的作用是:您的对象将自身检查到您排序的列表中的每个其他对象。
这是由一个函数完成的。 (不用担心 VS 在引用你的界面时会实现它,
请阅读上面的链接以获取更好的信息。
我知道我自己一开始在理解这一点时遇到了一些麻烦,因此对于任何额外的帮助,请添加评论,我将详细说明
what you need is a Sort function with a custom IComparer. What you have now is the default icomparer when you use sort. this will check on a field value.
When you create a custom IComparer (you do this in you class by implementing the Icomparable interface). what it does is: your object checks itself to every other object in the list you sort.
this is done by a function. (don't worry VS will implementd it when refering your interface
read on the links above for better information.
I know I had some troubles understanding this myself in the beginning, so for any extra help add a comment and I will elaborate
我通过创建
SortedList>
来完成此操作。每当我找到重复的键时,我只需将值插入与 SortedList 对象中已存在的键关联的现有列表中。这样,我就可以获得特定键的值列表。I did it by creating a
SortedList<int, List<string>>
. Whenever I find the duplicate key, I simply insert the value in the existing list associated with the key already present in the SortedList object. This way, I can have list of values for a particular key.使用您自己的比较器类!
如果排序列表中的键是整数,则可以使用以下比较器:
要使用 int 键和字符串值实例化新的 SortedList,请使用:
Use your own comparer class!
If your keys in the sorted list are integers, you may use for example this comparer:
To instanciate a new SortedList with int keys and string values use:
如果您并不真正关心具有相同键的元素的顺序,请将所有内容添加到列表中,然后按键对其进行排序:
如果您确实希望稍后插入的项目位于较早插入的项目之后,请在插入时对它们进行排序:
或者你可以有一个排序的列表列表:
我不确定你是否可以像我在上面第一个示例中那样在.NET 2.0中使用列表初始值设定项,但我确信你知道如何用数据填充列表。
If you don't really care about the sequence of the elements with equal keys, add everything to a list and then sort it by key:
If you really want the items inserted later to be after those inserted earlier, sort them as they are inserted:
Or you could have a sorted list of lists:
I'm not sure if you can use List initializers in .NET 2.0 like I did in the first example above, but I'm sure you know how to populate a list with data.
.NET 对稳定排序没有很大的支持(这意味着等价元素在排序时保持其相对顺序)。但是,您可以使用
List.BinarySearch
和自定义IComparer
编写自己的稳定排序插入(如果键小于 或等于目标,如果更大则+1)。请注意,
List.Sort
不是稳定的排序,因此您必须编写自己的稳定的快速排序例程,或者仅使用插入排序来初始填充集合。.NET doesn't have huge support for stable sorts (meaning that equivalent elements maintain their relative order when sorted). However, you can write your own stable-sorted-insert using
List.BinarySearch
and a customIComparer<T>
(that returns -1 if the key is less than or equal to the target, and +1 if greater).Note that
List.Sort
is not a stable sort, so you'd either have to write your own stable quicksort routine or just use insertion sort to initially populate the collection.您是否考虑过 NameValueCollection 类允许你为每个键存储多个值?例如,您可以具有以下内容:
然后检索您可以拥有的值:
给出输出:
'1' = 'one'
'1' = 'one bis'
'2' = 'two'
'2' = '两个的另一个值
“3”=“三”
did you contemplate the NameValueCollection class as it allows you to store multiple values per key? you could for example have the following:
and then to retrieve the values you could have:
which give the output:
'1' = 'one'
'1' = 'one bis'
'2' = 'two'
'2' = 'another value for two'
'3' = 'three'
在 .NET 2.0 中,您可以编写:
输出:
In .NET 2.0 you can write :
Output :
这个怎么样
How about this
我遇到了类似的问题,当时我正在设计一款类似于国际象棋游戏概念的游戏,让计算机走棋。我需要有多个棋子能够走棋的可能性,因此我需要有多个棋盘状态。每个棋盘状态都需要根据棋子的位置进行排名。为了便于论证和简单起见,假设我的游戏是圈和十字,我是圈,计算机是十字。如果棋盘状态显示连续 3 个“Nought”,那么这对我来说是最好的状态,如果它显示连续 3 个“X”,那么这对我来说是最差的状态,但对计算机来说是最好的状态。游戏中还有其他状态对其中一方更有利,而且还有多个状态会导致平局,那么当排名分数相等时,我该如何进行排名呢?这是我想出来的(如果您不是 VB 程序员,请提前道歉)。
我的比较器类:
我的声明:
我的 Board-State 实现:
正如您所看到的,RankScores 按降序排列,并且任何 2 个具有相同排名分数的状态都会位于底部,因为它始终具有更大的分配索引并且因此这允许重复。我还可以安全地调用 boardStates.Remove(myCurrentBoardState),它也使用比较器,并且比较器必须返回 0 值才能找到要删除的对象。
I had a similar issue where I was designing a game similar to the concept of a Chess game where you have the computer make a move. I needed to have the possibility of multiple pieces being able to make a move and thus I needed to have multiple Board-States. Each BoardState needed to be ranked based on the position of the pieces. For argument sake and simplicity, say my game was Noughts and Crosses and I was Noughts and the Computer was Crosses. If the board-state was showing 3 in a row of Noughts then this is the best state for me, if it shows 3 in a row of Crosses then this is the worst state for me and best for the computer. There are other states during the game that are more favourible to one or the other and furthermore there are muliplte states that result in a Draw, so how do I go about ranking it when there are equal rank scores. This is what I came up with (apologise in advance if you are not a VB programmer).
My comparer class:
My declarations:
My Board-State implementation:
As you can see RankScores are maintained in descending order and any 2 states having the same rank-score the later state goes to the bottom as it will always have a greater assigned Index and thus this allows duplicates. I can also safely call boardStates.Remove(myCurrentBoardState) which also uses the comparer and the comparer must return a 0 value in order to locate the objected to be deleted.