SortedList 未按键排序 - VB.NET
我需要对键值对进行排序,因此我决定使用 SortedList 而不是 HashTable。
我按以下顺序将数据添加到我的 SortedList 中,这是我需要的顺序。
Key | Value
--------------------------------
1 "700-800" | List(Of Object)
2 "900-1000" | List(Of Object)
3 "1100-1200" | List(Of Object)
4 "1700-1800" | List(Of Object)
5 "1900-2000" | List(Of Object)
键是字符串,值是对象列表。 键代表一个由两个整数值连接并用“-”分隔的时隙。 “700”作为字符串最初是 0700 一个整数。
例如,
Dim key As String = slotTimeStart.ToString() & "-" & slotTimeEnd.ToString()
但是一旦将这些键值对添加到 SortedList 中,它们就会按顺序出现。
3 "1100-1200" | List(Of Object)
4 "1700-1800" | List(Of Object)
5 "1900-2000" | List(Of Object)
1 "700-800" | List(Of Object)
2 "900-1000" | List(Of Object)
不幸的是,我收到的时间槽是两个无法更改的整数值。
有没有办法强制对 SortedList 进行排序? 或者这个问题是因为我存储密钥的方式造成的? 有没有更好的保存方法?
I have a the need for key value pair that I wish to sort so I decided to use a SortedList instead of a HashTable.
I am adding the data in the order below to my SortedList which is the order I need it in
Key | Value
--------------------------------
1 "700-800" | List(Of Object)
2 "900-1000" | List(Of Object)
3 "1100-1200" | List(Of Object)
4 "1700-1800" | List(Of Object)
5 "1900-2000" | List(Of Object)
The key is a string and the value is a List of objects. The key is representing a time slot that has been concatenated from two integer values and delimited by "-". "700" as a string was 0700 initially an integer.
e.g.
Dim key As String = slotTimeStart.ToString() & "-" & slotTimeEnd.ToString()
But once these key value pairs are added to the SortedList they appear in the order
3 "1100-1200" | List(Of Object)
4 "1700-1800" | List(Of Object)
5 "1900-2000" | List(Of Object)
1 "700-800" | List(Of Object)
2 "900-1000" | List(Of Object)
Unfortunately I recieve the times slots as two integer values which cannot be changed.
Is there any way to force a sort on a SortedList? or is this problem because of the way I am storing my key? Is there a better way to store it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建一个
SortedList(Of String, List(Of Object))
但传入
IComparer(Of String)
到 构造函数,其中实现将根据您想要的顺序比较键。您必须自己实现它,但这应该不会太难 - 只需用“-”分割字符串,用
Int32.Parse
解析两边并做出相应的反应。 如果您的键范围不重叠,您甚至可能不需要担心“-”之后的部分。编辑:这是一个演示。 它只打印出键,但这足以表明它们已按您想要的方式排序。
Create a
SortedList(Of String, List(Of Object))
but pass in anIComparer(Of String)
to the constructor, where the implementation will compare the keys according to the ordering you want.You'll have to implement that yourself, but it shouldn't be too hard - just split the string by '-', parse both sides with
Int32.Parse
and react accordingly. You may even not need to worry about the part after the '-', if your key ranges are non-overlapping.EDIT: Here's a demo. It only prints out the keys, but that's enough to show they're sorted as you want them.
看起来您是按字母顺序而不是数字顺序对其进行排序。 您必须将键设为数字才能获得您要查找的排序顺序。
It looks like you're sorting it alphabetically instead of numerically. You would have to make your key numeric to get the sort order you're looking for.
长度小于 4 位的时间需要添加零 ('0') 前缀,以使其与 4 位数字的时间长度相同。 这样,标准比较器将比较字符串 1 的字符 1(现在为 0)和字符串 2 的字符 1(现在为 1),并且字符串 1 将首先出现。
Times that are less than 4 digits long need to be prefixed with a zero ('0') to make them the same length as the ones with 4 digits. That way, the standard comparer will compare char 1 of string 1 which will now be a 0 to char 1 of string 2 which will be a 1, and string 1 will come out first.
键可以是十进制的并且看起来像
并根据需要转换并格式化为字符串。
could the keys be decimal and look like
and convert and format to string as needed.