c#.net tilde(ascii 126)对字符串进行排序 [英] c# .net tilde (ascii 126) sorting a string

查看:87
本文介绍了c#.net tilde(ascii 126)对字符串进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#.net tilde(ascii 126)当排序一个字符串似乎在1之前(ascii 49)这是正确的吗?

我,〜J,123,K,〜E,A,〜G,U

我期望订单是123,A,I,J,K,U,~E,~G,~J

不~E,~G,~J,123,A,I, K,U

c# .net tilde (ascii 126) when sorting a string seems to come before 1 (ascii 49) is this correct?
"I", "~J", "123", "K", "~E", "A", "~G", "U"
I expected the order to be 123, A, I, J, K, U, ~E, ~G, ~J
Not ~E, ~G, ~J, 123, A, I, K, U

推荐答案

C#是.NET,因此它默认为Unicode字符,而不是ASCII - 但是,这在非重音英语中几乎没有什么不同。



unicode字符顺序完全符合您的预期: http://unicode-table.com / en / [ ^ ]并快速检查:

C# is .NET, so it defaults to Unicode characters, not ASCII - however, this makes little different in unaccented English.

The unicode character order is exactly as you would expect: http://unicode-table.com/en/[^] and a quick check:
string[] data = {"I", "~J", "123", "K", "~E", "A", "~G", "U"};
Array.Sort(data);
foreach (string s in data)
    {
    Console.WriteLine(s);
    }

显示你所看到的内容!



但这是因为它使用的是默认的比较器,这不是一个序数比较 - 这是一个语言比较!

尝试使用序数比较:

shows exactly what you see!

But that's because it's using the default comparer, which is not an ordinal compare - it's a linguistic comparison!
Try using an Ordinal comparison:

string[] data = {"I", "~J", "123", "K", "~E", "A", "~G", "U"};
Array.Sort(data, StringComparer.Ordinal);
foreach (string s in data)
    {
    Console.WriteLine(s);
    }



你会得到你想要的结果:


And you will get the result you want:

123
A
I
K
U
~E
~G
~J


这篇关于c#.net tilde(ascii 126)对字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆