Consulting

Results 1 to 4 of 4

Thread: Help with sorting arrays numerically

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Help with sorting arrays numerically

    I am trying to figure out how to sort an array numerically. I have no problem getting it to work alphabetically; but numerically, it does not seem to work.

    I have below, for example, a list of four numbers divided by commas, but I can't understand why "actual" at the bottom of this code does not give "3, 4, 5, 11" instead of "11, 3, 4, 5". Many thanks in advance for any help on this matter!

    Dim actual As String
    Dim arr As Variant
    Dim overall As String
    Dim strTemp As Long
    Dim i As Long
    Dim j As Long
    Dim lngMin As Long
    Dim lngMax As Long
     
    overall = "3, 4, 11, 5"
    arr = Split(overall, ", ")
    
    lngMin = LBound(arr)
    lngMax = UBound(arr)
    For i = lngMin To lngMax - 1
      For j = i To lngMax
        If arr(i) > arr(j) Then
          strTemp = arr(j)
          arr(j) = arr(i)
          arr(i) = strTemp
        End If
      Next j
    Next i
    
    For i = 0 To UBound(arr)
      actual = Join(arr, ", ")
    Next i
    Last edited by Davecogz84; 04-25-2021 at 03:31 PM. Reason: Added code tags & formatting

Posting Permissions

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