Pergunta

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:

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.

Foi útil?

Solução

No need to reinvent the wheel :-)

Suppose an array of string named myArray:

Array.Sort(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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top