Array.Sort method places each lowercas letter before the uppercase.

Solitaire

Well-known member
Joined
Jun 28, 2004
Messages
465
Location
New York
Programming Experience
10+
The Array.Sort method sorts alphabetically when mixing upper and lowercase letters: lower first then upper for each letter separately.


When using a second optional parameter, it will sort using ASCII values, with all uppercase letters first.

Array.Sort(stringarray, StringComparer.Ordinal)


However, unable to sort alphabetically with upper first then lower for each letter separately. This is how it should be sorted and the way it's done in Excel.

VB.NET:
    Sub Main()
        Dim stary() As String = {"a", "b", "D", "A", "d", "c", "C", "B"}
        Array.Sort(stary)  'sorts alphabetically, lowercase first for each letter
        For x As Integer = 0 To 7
            Console.WriteLine(stary(x))
        Next

        Console.WriteLine()
        Array.Sort(stary, StringComparer.Ordinal)  'sorts using ASCII values, all uppercase letters first
        For x As Integer = 0 To 7
            Console.WriteLine(stary(x))
        Next
        Console.WriteLine()

        Array.Sort(stary, StringComparer.OrdinalIgnoreCase)  'same results as Array.Sort(stary)
        For x As Integer = 0 To 7
            Console.WriteLine(stary(x))
        Next
        Console.ReadLine()
    End Sub
 
This is how my Excel sorts: a,A,B,b,z,Z
With option case sensitive: a,A,b,B,z,Z
LibreOffice does the same also.

It's not ASCII by the way, it is Unicode.
 
The first 128 characters of Unicode are the same as ASCII (except maybe for the non-printing characters).

My version of Excel sorts like this: AaBbCcDd
 
Try
VB.NET:
Array.Sort(stary, StringComparer.CurrentCulture)

With my current culture this returns aAbBcCdD (still not what you wanted).
What CultureInfo is your CurrentCulture?
 
Back
Top