SortedList 未按键排序 - VB.NET

发布于 2024-07-15 04:11:43 字数 929 浏览 7 评论 0原文

我需要对键值对进行排序,因此我决定使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

递刀给你 2024-07-22 04:11:43

创建一个 SortedList(Of String, List(Of Object))但传入 IComparer(Of String)构造函数,其中实现将根据您想要的顺序比较键。

您必须自己实现它,但这应该不会太难 - 只需用“-”分割字符串,用 Int32.Parse 解析两边并做出相应的反应。 如果您的键范围不重叠,您甚至可能不需要担心“-”之后的部分。

编辑:这是一个演示。 它只打印出键,但这足以表明它们已按您想要的方式排序。

using System;
using System.Collections.Generic;

public class Test
{
    static void Main(string[] args)
    {
        var list = new SortedList<string, int>(new RangeComparer());
        list.Add("900-1000", 10);
        list.Add("1100-1200", 20);
        list.Add("700-800", 30);
        list.Add("1700-18000", 40);
        list.Add("1900-2000", 50);

        foreach (var entry in list)
        {
            Console.WriteLine(entry.Key);
        }
    }
}

public class RangeComparer : IComparer<string>
{
    private static int ParseStartOfRange(string range)
    {
        int hyphenIndex = range.IndexOf('-');
        // Normally do some error checking in case hyphenIndex==-1
        string firstPart = range.Substring(0, hyphenIndex);
        return int.Parse(firstPart);
    }

    public int Compare(string first, string second)
    {
        // In real code you would probably add nullity checks
        int firstStart = ParseStartOfRange(first);
        int secondStart = ParseStartOfRange(second);
        return firstStart.CompareTo(secondStart);
    }
}

Create a SortedList(Of String, List(Of Object)) but pass in an IComparer(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.

using System;
using System.Collections.Generic;

public class Test
{
    static void Main(string[] args)
    {
        var list = new SortedList<string, int>(new RangeComparer());
        list.Add("900-1000", 10);
        list.Add("1100-1200", 20);
        list.Add("700-800", 30);
        list.Add("1700-18000", 40);
        list.Add("1900-2000", 50);

        foreach (var entry in list)
        {
            Console.WriteLine(entry.Key);
        }
    }
}

public class RangeComparer : IComparer<string>
{
    private static int ParseStartOfRange(string range)
    {
        int hyphenIndex = range.IndexOf('-');
        // Normally do some error checking in case hyphenIndex==-1
        string firstPart = range.Substring(0, hyphenIndex);
        return int.Parse(firstPart);
    }

    public int Compare(string first, string second)
    {
        // In real code you would probably add nullity checks
        int firstStart = ParseStartOfRange(first);
        int secondStart = ParseStartOfRange(second);
        return firstStart.CompareTo(secondStart);
    }
}
流绪微梦 2024-07-22 04:11:43

看起来您是按字母顺序而不是数字顺序对其进行排序。 您必须将键设为数字才能获得您要查找的排序顺序。

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.

梦忆晨望 2024-07-22 04:11:43

长度小于 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.

时光病人 2024-07-22 04:11:43

键可以是十进制的并且看起来像

7.08
9.1
11.12
17.18
19.20

并根据需要转换并格式化为字符串。

    Dim sl As New SortedList(Of Decimal, Object)
    'sample data
    For x As Integer = 7 To 20 Step 2
        sl.Add(CDec(x + ((x + 1) / 100)), New Object)
    Next

    Dim aKey As Decimal
    Dim slotStart As DateTime = #1:00:00 PM#
    Dim slotEnd As DateTime = #2:00:00 PM#

    aKey = CDec(slotStart.Hour + (slotEnd.Hour / 100))
    sl.Item(aKey) = New Object

could the keys be decimal and look like

7.08
9.1
11.12
17.18
19.20

and convert and format to string as needed.

    Dim sl As New SortedList(Of Decimal, Object)
    'sample data
    For x As Integer = 7 To 20 Step 2
        sl.Add(CDec(x + ((x + 1) / 100)), New Object)
    Next

    Dim aKey As Decimal
    Dim slotStart As DateTime = #1:00:00 PM#
    Dim slotEnd As DateTime = #2:00:00 PM#

    aKey = CDec(slotStart.Hour + (slotEnd.Hour / 100))
    sl.Item(aKey) = New Object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文