Sort array or sort string other way

Temporary-Failure

Board Regular
Joined
Jul 16, 2010
Messages
140
I have cell A1 which has string: -18 -11 -15 -8 -2 -5 -20 -14 -10 -7 0 -13 -19 -4 -1 -12 -6 -3 -9
I need to sort this string to be like: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20

Or string: 18 11 15 8 2 5 20 14 10 7 0 13 19 4 1 12 6 3 9
To be like 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

There's nice sorting way here: https://stackoverflow.com/questions/152319/vba-array-sort-function
It sorts ie. 22 29 21 25 23 27 24 26 20 28 30 nicely but it won't work in my cases.

I don't find solution. Could you help me? It doesn't have to be vba it can be also excel function if possible.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
How about
Code:
Function Sortstring(St As String, Ord As String) As String
   Dim Sp As Variant, tmp As Variant
   Dim i As Long, j As Long
   
   Sp = Split(St)
   For i = 0 To UBound(Sp)
      For j = i To UBound(Sp)
         If Ord = "a" Then
            If CLng(Sp(i)) > CLng(Sp(j)) Then
               tmp = Sp(i)
               Sp(i) = Sp(j)
               Sp(j) = tmp
            End If
         Else
            If CLng(Sp(i)) < CLng(Sp(j)) Then
               tmp = Sp(i)
               Sp(i) = Sp(j)
               Sp(j) = tmp
            End If
         End If
      Next j
   Next i
   Sortstring = join(Sp, " ")
End Function
Used like


Excel 2013/2016
AB
1-18 -11 -15 -8 -2 -5 -20 -14 -10 -7 0 -13 -19 -4 -1 -12 -6 -3 -90 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -18 -19 -20
218 11 15 8 2 5 20 14 10 7 0 13 19 4 1 12 6 3 90 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 18 19 20
Sheet1
Cell Formulas
RangeFormula
B1=Sortstring(A1,"d")
B2=Sortstring(A2,"a")
 
Upvote 0
Another option depending on your data & required output
Code:
Function Sortstring(St As String) As String
   Dim Sp As Variant, tmp As Variant
   Dim i As Long, j As Long
   
   Sp = Split(St)
   For i = 0 To UBound(Sp)
      For j = i To UBound(Sp)
         If Abs(CLng(Sp(i))) > Abs(CLng(Sp(j))) Then
            tmp = Sp(i)
            Sp(i) = Sp(j)
            Sp(j) = tmp
         End If
      Next j
   Next i
   Sortstring = join(Sp, " ")
End Function
Used =Sortstring(A1)
 
Upvote 0
This is another sorting function. Note the SortAsNumbers argument can be set depending on whether you want "10" < "2" or 2 < 10
Code:
Function SortDelimitedString(ByVal aString As String, Optional SortAsNumbers As Boolean = False, _
                        Optional Delimiter As String = " ", Optional Descending As Boolean = False) As String
    Dim Words As Variant
    Dim strLeft As String, Pivot As String, strRight As String
    Dim i As Long, LT As Boolean

    Words = Split(aString, Delimiter)
    Pivot = Words(0)

    For i = 1 To UBound(Words)
        LT = Words(i) < Pivot
        If SortAsNumbers And IsNumeric(Pivot) And IsNumeric(Words(i)) Then
            LT = Val(Words(i)) < Val(Pivot)
        End If

        If LT Xor Descending Then
            strLeft = strLeft & Delimiter & Words(i)
        Else
            strRight = strRight & Delimiter & Words(i)
        End If
    Next i

    strLeft = Mid(strLeft, Len(Delimiter) + 1)
    strRight = Mid(strRight, Len(Delimiter) + 1)
    
    If 0 < Len(strLeft) Then
        Pivot = SortDelimitedString(strLeft, SortAsNumbers, Delimiter, Descending) & Delimiter & Pivot
    End If
    If 0 < Len(strRight) Then
        Pivot = Pivot & Delimiter & SortDelimitedString(strRight, SortAsNumbers, Delimiter, Descending)
    End If
    SortDelimitedString = Pivot
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,168
Members
448,870
Latest member
max_pedreira

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