Sorting a Cell (string) In Ascending order

Avaya

New Member
Joined
Oct 9, 2009
Messages
3
Hi all,

I'd appreciate any help that can be provided in sorting a single cell (string) in an ascending result.

For instance, the cell value would contain "8009543200".
The result I'd like returned "0000234589".

Again, thanks for your help.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Welcome to the board.

Code:
Function AnaSort(sInp As String, Optional bUniq As Boolean = False) As String
    ' shg 2008-1216
 
    ' Returns the characters of sInp in alpha order: AnaSort("amaze") = "aaemz"
    ' with option to return unique characters only:  AnaSort("amaze", True) = "aemz"
    Dim i       As Long             ' index to string characters
    Dim aiNum(0 To 255) As Long     ' count of characters by code
    Dim iAsc    As Long             ' index to aiNum
 
    For i = 1 To Len(sInp)
        iAsc = Asc(Mid(sInp, i, 1))
        aiNum(iAsc) = aiNum(iAsc) + 1
    Next i
 
    If bUniq Then
        For iAsc = 0 To 255
            If aiNum(iAsc) Then AnaSort = AnaSort & Chr$(iAsc)
        Next iAsc
    Else
        For iAsc = 0 To 255
            AnaSort = AnaSort & String$(aiNum(iAsc), Chr$(iAsc))
        Next iAsc
    End If
End Function

E.g., =AnaSort(A1)
 
Last edited:
Upvote 0
You're welcome, glad it worked for you. Here's another alternative:

Code:
Function StrSort(sInp As String, Optional bCaseSens As Boolean = False) As String
    ' shg 2008
    ' Insertion-sorts sInp
    
    Dim i As Long
    Dim j As Long
    Dim s As String
    
    If bCaseSens Then StrSort = sInp Else StrSort = LCase(sInp)
    
    For i = 2 To Len(sInp)
        s = Mid(StrSort, i, 1)
        
        For j = i - 1 To 1 Step -1
            If Mid(StrSort, j) < s Then Exit For
            Mid(StrSort, j + 1) = Mid(StrSort, j, 1)
        Next j
        
        Mid(StrSort, j + 1) = s
    Next i
End Function
 
Upvote 0
Worked perfect.... Seems the latest will lower case the result though - no biggie.. Thanks again
 
Upvote 0
This is another

Code:
Function SortedString(aString As String, Optional Descending As Boolean) As String
    Dim strLeft As String, strRight As String
    Dim chrPivot As String, chrVar As String
    Dim i As Long
    
    chrPivot = Left(aString, 1)
    
    For i = 2 To Len(aString)
        chrVar = Mid(aString, i, 1)
        If ((StrComp(chrVar, chrPivot, vbTextCompare) = -1) Or _
            ((StrComp(chrVar, chrPivot, vbTextCompare) = 0) And (chrVar < chrPivot))) Xor Descending Then
            strLeft = chrVar & strLeft
        Else
            strRight = strRight & chrVar
        End If
    Next i
    
    If 1 < Len(strLeft) Then strLeft = SortedString(strLeft)
    If 1 < Len(strRight) Then strRight = SortedString(strRight)
    SortedString = strLeft & chrPivot & strRight

End Function
 
Upvote 0
Seems the latest will lower case the result though
Easy to change;

Code:
Function StrSort(sInp As String, Optional bCaseSens As Boolean = False) As String
    ' shg 2008, 2011
    ' Insertion-sorts sInp
 
    Dim i         As Long
    Dim j         As Long
    Dim s         As String
    Dim iComp     As Long
 
    StrSort = sInp
    iComp = IIf(bCaseSens, vbBinaryCompare, vbTextCompare)
 
    For i = 2 To Len(sInp)
        s = Mid(StrSort, i, 1)
        For j = i - 1 To 1 Step -1
            If StrComp(Mid(StrSort, j, 1), s, iComp) <= 0 Then Exit For
            Mid(StrSort, j + 1) = Mid(StrSort, j, 1)
        Next j
        Mid(StrSort, j + 1) = s
    Next i
End Function
 
Upvote 0
here is not so clean but none macro version.


Excel Workbook
AB
13OriginalSorted
1480095432000000234589
1524390701000000123479
16207503201000000122357
Sheet5
 
Upvote 0
If the desired result is a text value with the leading zeros, you can modify snoopyhr's neat array formula to add the missing leading zeros as follows:

Code:
=[COLOR=red]RIGHT(REPT("0",LEN(A14))&[/COLOR]SUM(SMALL(MID($A14,ROW(INDIRECT("1:"&LEN($A14))),1)*1,ROW(INDIRECT("1:"&LEN($A14))))/10^ROW(INDIRECT("1:"&LEN($A14))))*MAX(10^ROW(INDIRECT("1:"&LEN($A14))))[COLOR=red],LEN(A14))[/COLOR]

Mike
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top