如何使用变音符号对列表进行排序而不删除变音符号

发布于 2024-10-26 08:12:06 字数 312 浏览 4 评论 0原文

如何对包含带变音符号的字母的列表进行排序?

本例中使用的单词是虚构的。

现在我得到一个显示此内容的列表:

  • 巴布
  • 巴兹
  • 贝兹

但我想获得一个显示此内容的列表:

  • 巴兹
  • 巴布
  • 贝兹

将变音符号单独显示为一个字母。 有没有办法在 C# 中做到这一点?

How to sort a list that contains letters with diacritic markings?

Words used in this example are made up.

Now I get a list that displays this:

  • báb
  • baz
  • bez

But I want to get a list that displays this:

  • baz
  • báb
  • bez

Showing the diacritic as a letter on its own.
Is there a way to do this in C#?

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

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

发布评论

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

评论(1

左秋 2024-11-02 08:12:06

如果您将当前线程的区域性设置为您想要排序的语言,那么这应该会自动工作(假设您不需要某些特殊的自定义排序顺序)。像这样

List<string> mylist;
....
Thread.CurrentThread.CurrentCulture = new CultureInfo("pl-PL");
mylist.Sort();

应该让您根据波兰文化设置排序列表。

更新:如果区域性设置没有按照您想要的方式对其进行排序,那么另一个选择是实现您自己的字符串比较器。

更新 2:字符串比较器示例:

public class DiacriticStringComparer : IComparer<string>
{
    private static readonly HashSet<char> _Specials = new HashSet<char> { 'é', 'ń', 'ó', 'ú' };

    public int Compare(string x, string y)
    {
        // handle special cases first: x == null and/or y == null,  x.Equals(y)
        ...

        var lengthToCompare = Math.Min(x.Length, y.Length);
        for (int i = 0; i < lengthToCompare; ++i)
        {
            var cx = x[i];
            var cy = y[i];

            if (cx == cy) continue;

            if (_Specials.Contains(cx) || _Specials.Contains(cy))
            {
                // handle special diacritics comparison
                ...
            }
            else
            {
                // cx must be unequal to cy -> can only be larger or smaller
                return cx < cy ? -1 : 1;
            }
        }
        // once we are here the strings are equal up to lengthToCompare characters
        // we have already dealt with the strings being equal so now one must be shorter than the other
        return x.Length < y.Length ? -1 : 1;
    }
}

免责声明:我尚未测试它,但它应该为您提供总体思路。另外 char.CompareTo() 不会按字典顺序进行比较,而是根据我发现的一个来源进行比较和>确实 - 但不能保证。最坏的情况是,您必须将 cx 和 cy 转换为字符串,然后使用默认的字符串比较。

If you set the culture of the current thread to the language you want to sort for then this should work automagically (assuming you don't want some special customized sort order). Like this

List<string> mylist;
....
Thread.CurrentThread.CurrentCulture = new CultureInfo("pl-PL");
mylist.Sort();

Should get you the list sorted according to the Polish culture settings.

Update: If the culture settings don't sort it the way you want then another option is to implement your own string comparer.

Update 2: String comparer example:

public class DiacriticStringComparer : IComparer<string>
{
    private static readonly HashSet<char> _Specials = new HashSet<char> { 'é', 'ń', 'ó', 'ú' };

    public int Compare(string x, string y)
    {
        // handle special cases first: x == null and/or y == null,  x.Equals(y)
        ...

        var lengthToCompare = Math.Min(x.Length, y.Length);
        for (int i = 0; i < lengthToCompare; ++i)
        {
            var cx = x[i];
            var cy = y[i];

            if (cx == cy) continue;

            if (_Specials.Contains(cx) || _Specials.Contains(cy))
            {
                // handle special diacritics comparison
                ...
            }
            else
            {
                // cx must be unequal to cy -> can only be larger or smaller
                return cx < cy ? -1 : 1;
            }
        }
        // once we are here the strings are equal up to lengthToCompare characters
        // we have already dealt with the strings being equal so now one must be shorter than the other
        return x.Length < y.Length ? -1 : 1;
    }
}

Disclaimer: I haven't tested it but it should give you the general idea. Also char.CompareTo() does not compare lexicographically but according to one source I found < and > does - can't guarantee it though. Worst case you have to convert cx and cy into strings and then use the default string comparison.

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