Results 1 to 4 of 4

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

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    465
    Reputation
    228

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

    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

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,209
    Reputation
    2859
    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.
    [xcode=vb] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    465
    Reputation
    228
    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

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    140
    Reputation
    159
    Try
    Code:
    Array.Sort(stary, StringComparer.CurrentCulture)
    With my current culture this returns aAbBcCdD (still not what you wanted).
    What CultureInfo is your CurrentCulture?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •