I am having an input array defined as follows:
string[] inputArray = { "a", "s", "d", "f", "g", "2", "3", "34", "4", "4", "y", "a", "f", "8", "h", "m" };
When i perform Sorting over this array i am getting the output as:
{"2","3","34","4","4","8","a","a","d","f","f","g","h","m","s","y"}
Why the numbers comes before alphabets?
Is Array.Sort()
performs sorting on the basics of ASCII code(ascii code of numbers are lower than letters)?
答案 0 :(得分:3)
因为Array.Sort
uses the default comparer才能排序。并且comparer将根据当前文化比较两个字符串(请注意,当双方都是字符串时,比较器具有故障转移机制)。这通常会根据他们的字符代码进行比较,然后数字会出现在字母之前。
答案 1 :(得分:1)
字符串在所谓的词典排序中进行比较,这意味着您要比较两个第一个字符,如果相等则比较接下来的两个字符,依此类推。如果其中一个字符串在另一个字符串之前结束,则它首先出现。
确实比较了两个字符的数值,通常是ASCII。在ASCII表中,控制字符(不可打印)首先出现,然后是数字,大写字母,小写字母和这些组之间的特殊字符。
答案 2 :(得分:0)
它的逻辑基于IComparable接口。因此sort方法使用的默认实现更喜欢数字到字母表。
第二:它完全基于IComparable接口,您可以在here
中看到文档