如何使数组。区分大小写?

时间:2022-06-21 07:20:36

Basically I need to sort a list of strings, but there are lower case, upper case and numerics in the strings. Currently when it sorts them it values a and A as the same letter so:

基本上我需要对字符串进行排序,但是字符串中有小写、大写和数字。目前,当它对它们进行排序时,它将a和a的值定为相同的字母,所以:

acfgh
aztyu
Ahtwm

would be sorted to:

将排序:

acfgh
Ahtwm
aztyu

But I want it to rank capitals before lower case (and numerics before capitals) so that I would get:

但是我想让它在小写之前给大写字母排序(在大写之前给数字排序)这样我就能得到:

Ahtwm
acfgh
aztyu

I'd rather still use the methods already contained in the library than create my own quicksort.

我宁愿使用库中已经包含的方法,也不愿创建自己的快速排序。

1 个解决方案

#1


6  

No need to reinvent the wheel :-)

没有必要重新发明*:-)

Suppose an array of string named myArray:

假设有一个名为myArray的字符串数组:

Array.Sort(myArray, StringComparer.Ordinal)

数组中。排序(myArray StringComparer.Ordinal)

will do the trick (capitals before lower case and numerics before capitals).

将会做这个魔术(在小写和数字前大写)。

Example:

例子:

Dim str = String() = {"aa", "Aa", "1a", "1A"}
Array.Sort(str, StringComparer.Ordinal)

Output:

输出:

1A
1a
Aa
aa

#1


6  

No need to reinvent the wheel :-)

没有必要重新发明*:-)

Suppose an array of string named myArray:

假设有一个名为myArray的字符串数组:

Array.Sort(myArray, StringComparer.Ordinal)

数组中。排序(myArray StringComparer.Ordinal)

will do the trick (capitals before lower case and numerics before capitals).

将会做这个魔术(在小写和数字前大写)。

Example:

例子:

Dim str = String() = {"aa", "Aa", "1a", "1A"}
Array.Sort(str, StringComparer.Ordinal)

Output:

输出:

1A
1a
Aa
aa