Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不删除变音符号的情况下使用变音符号对列表进行排序_C#_Sorting_Diacritics - Fatal编程技术网

C# 如何在不删除变音符号的情况下使用变音符号对列表进行排序

C# 如何在不删除变音符号的情况下使用变音符号对列表进行排序,c#,sorting,diacritics,C#,Sorting,Diacritics,如何对包含带变音符号的字母的列表进行排序 本例中使用的单词都是虚构的 现在,我得到一个列表,其中显示: báb 巴兹 贝兹 但我想得到一个显示以下内容的列表: 巴兹 báb 贝兹 将变音符号单独显示为字母。 在C#?中有没有办法做到这一点?如果您将当前线程的区域性设置为要排序的语言,那么这应该自动工作(假设您不需要特殊的自定义排序顺序)。像这样 List<string> mylist; .... Thread.CurrentThread.CurrentCulture = ne

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

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

现在,我得到一个列表,其中显示:

  • báb
  • 巴兹
  • 贝兹
但我想得到一个显示以下内容的列表:

  • 巴兹
  • báb
  • 贝兹
将变音符号单独显示为字母。
在C#?

中有没有办法做到这一点?如果您将当前线程的区域性设置为要排序的语言,那么这应该自动工作(假设您不需要特殊的自定义排序顺序)。像这样

List<string> mylist;
....
Thread.CurrentThread.CurrentCulture = new CultureInfo("pl-PL");
mylist.Sort();
列出mylist;
....
Thread.CurrentThread.CurrentCulture=新的CultureInfo(“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;
    }
}
public类DiacriticStringComparer:IComparer
{
private static readonly HashSet_Specials=新HashSet{'é',ń',ó',ú'};
公共整数比较(字符串x、字符串y)
{
//首先处理特殊情况:x==null和/或y==null,x.Equals(y)
...
var lengthToCompare=数学最小值(x.长度,y.长度);
对于(int i=0;i只能大于或小于
返回cx

免责声明:我还没有测试它,但它应该给你一个大致的想法。另外,
char.CompareTo()
不按字典顺序进行比较,但根据我找到的一个来源,确实进行了比较,但不能保证这一点。最糟糕的情况是,您必须将
cx
cy
转换为字符串,然后使用默认的字符串比较。

更新:不在alfabet末尾加上变音符号。这是某种语言中的标准排序顺序吗?我对波兰语的简单测试返回
báb,baz,贝兹
我想要一个有以下字母的文化:é,ń,ó&ú。并将它们分别放在e、n、o和u之后。我发现冰岛人有四个字母中的三个,但我想要一个有四个字母的。有人知道这样的文化吗?你能给我举一个这样的字符串比较器的例子吗?@Mat我忍不住要说,“找到一个排序规则有效的文化”就像用铲子翻煎饼一样。这里可能有更多信息,您可能希望在此搜索更多信息。我在谷歌搜索中发现了as#1。@Shibumi我不想像你写的链接中所说的那样删除发音符号。我知道没有这样的文化。这就是为什么我要求另一种方法来做这件事,比如克里斯乌写的方法作为答案@谢谢你的帮助,我要试试。