Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 - Fatal编程技术网

C# 字符串排序

C# 字符串排序,c#,sorting,C#,Sorting,我将以下C#代码编译为Sort.exe: using System; using System.Collections.Generic; class Test { public static int Main(string[] args) { string text = null; List<string> lines = new List<string>(); while((text = Console.

我将以下C#代码编译为Sort.exe:

using System;
using System.Collections.Generic;

class Test
{
    public static int Main(string[] args)
    {
        string text = null;
        List<string> lines = new List<string>();
        while((text = Console.In.ReadLine()) != null)
        {
            lines.Add(text);
        }

        lines.Sort();

        foreach(var line in lines)
            Console.WriteLine(line);

        return 0;
    }
}
现在,如果在命令提示符下运行它,输出如下:

C:\Users\girijesh\AppData\Local\Temp>sort < input.txt
x000000000000000000037.000000000
x000000000000000000093.000000000
x-00000000000000000020.000000000
x000000000000000000538.000000000
x000000000000000100000.000000000
C:\Users\girijesh\AppData\Local\Temp>sort
我无法理解以
x-
开头的字符串(输出中的第三行)位于以
x0
开头的字符串中间时是什么类型的字符串排序。第三行应该在顶部或底部。Excel也显示了同样的行为。

在许多区域性(包括不变区域性)中,连字符是一个字符,对于排序目的来说,它的重要性很小。在大多数文本中,这是有道理的:
pre-which
prewhich
非常相似。例如,下面的列表按如下顺序排列,我认为这很好:

preasdf
prewhatever
pre-whatever
prezxcv
您似乎需要一个比较,其中的值纯粹通过其unicode代码点值进行比较。如果将行更改为:

lines.Sort(StringComparer.Ordinal);
那么您的结果是:

x-00000000000000000020.000000000
x000000000000000000037.000000000
x000000000000000000093.000000000
x000000000000000000538.000000000
x000000000000000100000.000000000

如果你想知道为什么<代码> - 20 值结束了,那么考虑一下如果删除了<代码> ->代码>它会是什么样子(和上面的代码>预列表)相比。


如果您的输入总是采用
x[某些数字]
格式,我会将
x
之后的值解析为
十进制
双精度
,并对其进行排序。这将更容易确保预期的行为,并且总体上更好。

那么,您是在询问排序字符串的内部工作吗?或者如何对其进行排序,使列表显示为第一个列表?使用
lines=lines.OrderBy(line=>line.ToList()时会发生什么
?@Nahuell他在问为什么标准排序函数似乎给出了不正确的结果。如果您去掉文件处理部分,只填充
列表
内联,您的问题会更好(IMO)。我现在自己也有这样的代码,因为它更容易测试-你愿意让我更新这个问题吗?请参阅。基本上,连字符被视为“可忽略的”。因此
-
字符被认为是不正确的?我的意思是,没有
ordinal
string
x-00000000000000000020
x00000000000020
?@Rahul这是我最初的想法,但它并不完全正确(这是用于软连字符,而不是常规连字符)。我修改了我的回答,以澄清如何对待连字符。它们的重要性较低,但不是没有。你的答案没有错,解释得很好,但我仍然不清楚sort在这种情况下是如何处理的
-
?我认为这相当于(在这里相对简单的示例中):首先,比较不包含连字符的字符串。如果字符串不同,则使用该比较作为答案;如果它们是相同的,那么带连字符的一个在不带连字符的一个之后。这就是为什么
prewhater
prewhater
彼此相邻,并且介于其他两个之间;我认为sort首先考虑的是[a-z][a-z][0-9],然后是像-//等
x-00000000000000000020.000000000
x000000000000000000037.000000000
x000000000000000000093.000000000
x000000000000000000538.000000000
x000000000000000100000.000000000
x000000000000000000037.000000000
x000000000000000000093.000000000
x00000000000000000020.000000000
x000000000000000000538.000000000
x000000000000000100000.000000000