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.

Code:
    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
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,377
Location
Norway
Programming Experience
10+
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.
 

Solitaire

Well-known member
Joined
Jun 28, 2004
Messages
465
Location
New York
Programming Experience
10+
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
 

Don Delegate

Well-known member
Joined
Jan 4, 2006
Messages
140
Location
Belgium
Programming Experience
10+
Try
Code:
Array.Sort(stary, StringComparer.CurrentCulture)
With my current culture this returns aAbBcCdD (still not what you wanted).
What CultureInfo is your CurrentCulture?
 
Top Bottom