排序字典 - C#

发布于 2024-10-12 12:18:35 字数 325 浏览 2 评论 0原文

我试图弄清楚如何创建一个排序字典,其中键以非字母顺序排序。有没有办法可以定义我想要的排序方式?

例如,键的顺序可能如下:

AAA1X
AAB1Y
AAC1Y
AAA2X
AAB2Y
AAC2X

虽然前三个字母是按字母顺序排列的,但如果我按原样排序,它们将以错误的顺序排列(由于数字)。另请注意,末尾有 X 或 Y。在代码中,只会有一个 X 或一个 Y。

即使我可以编写一个枚举来排序所有可能的组合,我也愿意这样做,但我不确定如何使用排序的字典和枚举...

我知道这有点模糊,但任何帮助将不胜感激!

干杯!

I'm trying to figure out how to create a sorted dictionary where the key is sorted in a non-alphabetical manner. Is there a way I can define the way I want it to sort?

For example, the keys might be in order like the following:

AAA1X
AAB1Y
AAC1Y
AAA2X
AAB2Y
AAC2X

Although the first three letters are alphabetical, if I sort as is it will lump them in the wrong order (due to the number). Also note, that there is either a X or Y at the end. In the code, there will only ever be an X or a Y.

Even if I can write an enumeration for the ordering of all possible combinations I'd be willing to do that as well, but I'm not sure how I can use the sorted dictionary and an enumeration...

I know this is a bit vague, but any help would be much appreciated!

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

烏雲後面有陽光 2024-10-19 12:18:35

SortedDictionary 的构造函数之一采用 IComparer,您可以在其中指定字典将用于排序的自定义比较器类。

public class CustomComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        // do your own comparison however you like; return a negative value
        // to indicate that x < y, a positive value to indicate that x > y,
        // or 0 to indicate that they are equal.
    }
}

...

SortedDictionary<string, object> dict = 
              new SortedDictionary<string, object>(new CustomComparer());

One of the constructors for SortedDictionary<TKey, TValue> takes an IComparer<TKey>, where you can specify a custom comparer class that the dictionary will use for sorting.

public class CustomComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        // do your own comparison however you like; return a negative value
        // to indicate that x < y, a positive value to indicate that x > y,
        // or 0 to indicate that they are equal.
    }
}

...

SortedDictionary<string, object> dict = 
              new SortedDictionary<string, object>(new CustomComparer());
把人绕傻吧 2024-10-19 12:18:35

您可以:

创建一个类来封装这些键并重写 == 运算符、Object.Equals 方法和 Object.GetHashCode 方法。还让该类实现 IComparable 接口,以便平等规则自动以正确的方式对您的键进行排序。现在,您只需将这些键放入 SortedDictionarySortedList,它会自动按照您想要的方式排序。

实现一个 IComparer 对象来比较这些对象按照您想要的方式字符串,并在构造 SortedDictionary 时指定 IComparer 或排序列表

You can :

Create a class that encapsulates those keys and override the == operator, Object.Equals method and the Object.GetHashCode method. Also have the class implement the IComparable interface such that the rules for equality will automatically sort your keys the right way. Now you can simply place those keys in a SortedDictionary or SortedList and it will automatically sort the way you want.

OR

Implement an IComparer object that compares those strings the way you want, and specify the IComparer when constructing a SortedDictionaryor SortedList.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文