Here's my submission to sort a string of characters in numeric, then lowercase, then uppercase. It may be useful, it may be not.

Function SortStringUpperLower(ByVal strString As String) As String


            Dim arrString() As Char = Nothing
            Dim strReturn As String = String.Empty
            Dim chrPrevious As Char = CChar(String.Empty)

            Dim arrLower() As Char = Nothing
            Dim arrUpper() As Char = Nothing

            ReDim arrString(Len(strString) - 1)
            arrString = strString.ToCharArray

            'The string "i a 2 M 1 c D 3 ß B E 4 x g K q A m" would become "1234ABDEKMacgimqxß" on sorting the array.

            Dim strNumeric As String = String.Empty
            Dim strLowerCase As String = String.Empty
            Dim strUpperCase As String = String.Empty

            'The characters are placed in 3 seperate strings which hold either numerical or lowercase or uppercase characters.

            For i = 1 To 3

                For Each ch As Char In arrString

                    If ch.ToString <> " " Then

                        Select Case i

                            Case Is = 1 ' Only append numerical characters.
                                If IsNumeric(ch) Then
                                    strNumeric += ch.ToString + " "
                                End If
                            Case Is = 2 ' Only append lower case characters.

                                If Asc(ch) >= 97 Then
                                    strLowerCase += ch.ToString + " "
                                End If

                            Case Is = 3 ' Only append upper case characters.

                                If Asc(ch) >= 65 And Asc(ch) <= 90 Then
                                    strUpperCase += ch.ToString + " "
                                End If

                        End Select

                    End If ' If ch.ToString <> " " Then

                Next ' For Each ch As Char In arrString

            Next ' For i = 1 to 3

            'The return string need to be hold the numeric data first then the alphabet data in order (lowercase then uppercase for matching characters).

            'Therefore the return string for "i a 2 M 1 c D 3 ß B E 4 x g K q A m" would become "1 2 3 4 a A B c D E g i K m M q x ß".

            strReturn = strNumeric

            'Position upper case character next to its lower case character.

            strUpperCase = LCase(strUpperCase)

            For Each chrLower As Char In strLowerCase

                If chrLower.ToString <> " " Then

                    For Each chrUpper As Char In strUpperCase

                        Dim blnHasLowerChar As Boolean = False

                        If chrUpper.ToString <> " " Then

                            If Asc(chrUpper) < Asc(chrLower) Then
                                strReturn += UCase(chrUpper).ToString + " "
                                'Remove inserted character from the string containing upper case characters.
                                strUpperCase = Replace(strUpperCase, chrUpper.ToString, "")

                            End If

                        End If ' If chrUpper.ToString <> " " Then

                    Next ' For Each chrUpper As Char In strUpperCase

                    strReturn += chrLower.ToString + " "

                End If ' If chrLower.ToString <> " " Then

            Next ' For Each chrLower As Char In strLowerCase

            'There maybe some uppercase characters still to append.
            For Each chrUpper As Char In strUpperCase

                If chrUpper.ToString <> " " Then

                    strReturn += chrUpper.ToString

                    Exit For

                End If


            strReturn = Trim(strReturn)

            SortStringUpperLower = strReturn

        Catch ex As Exception

        End Try

    End Function

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).


This thread is over 3 years old! If you have some code you'd like to share, you can start a new thread. Just be sure to use code tags to wrap your code. (See the (code) button).

On that note, consider this example to streamline what you are doing.

Imports System.Linq
Imports System.Xml.Linq

Module Module1

    Sub Main()

        Dim input As String = "i a 2 M 1 c D 3 ß B E 4 x g K q A m"
        Dim temp = input.ToCharArray().OrderBy(Function(c As Char) Char.ToUpper(c))
        Dim sortedInput = String.Join(" ", temp.Where(Function(c As Char) c <> " "c).Select(Function(c As Char) c.ToString()).ToArray())

    End Sub

End Module

Edited 4 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.