2

I have array with the following values:

arr_ReportsData = Array("0_1 Company Overview", "10_1 Rank by Locations", "1_1 Trend by Business Units", "2_1 Trend by Questions", "11_1 Rank by Locations")

I need to order them naturally (alphanumerically).

Expected output:

  • 0_1 Report1
  • 1_1 Report2
  • 2_1 Report3
  • 10_1 Report4
  • 11_1 Report5

I tried bubble sort and quick sort, but the result for both algorithms of course was alphabetical ordering.

Actual output:

  • 0_1 Report1
  • 10_1 Report4
  • 11_1 Report5
  • 1_1 Report2
  • 2_1 Report3

Problem: I can't seem to find anything like an actual algorithm for this case. Should I write some workaround like splitting the numbers and parsing to integer and then sorting?

This is what I tried and is working (but not for my case):

'Bubble sort
For i = LBound(arr_ReportsData) to UBound(arr_ReportsData)
  For j = LBound(arr_ReportsData) to UBound(arr_ReportsData) - 1
      If arr_ReportsData(j) > arr_ReportsData(j + 1) Then
         TempValue = arr_ReportsData(j + 1)
         arr_ReportsData(j + 1) = arr_ReportsData(j)
         arr_ReportsData(j) = TempValue
      End If
  Next
Next



Sub QuickSort (vec,loBound,hiBound)

  Dim pivot,loSwap,hiSwap,temp,counter
  '== Two items to sort
  if hiBound - loBound = 1 then
    if vec(loBound) > vec(hiBound) then
      Call SwapRows(vec,hiBound,loBound)
    End If
  End If

  '== Three or more items to sort
    pivot = vec(int((loBound + hiBound) / 2))
    vec(int((loBound + hiBound) / 2)) = vec(loBound)
    vec(loBound) = pivot

  loSwap = loBound + 1
  hiSwap = hiBound
  Do
    '== Find the right loSwap
    while loSwap < hiSwap and vec(loSwap) <= pivot
      loSwap = loSwap + 1
    wend
    '== Find the right hiSwap
    while vec(hiSwap) > pivot
      hiSwap = hiSwap - 1
    wend
    '== Swap values if loSwap is less then hiSwap
    if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap)


  Loop While loSwap < hiSwap

    vec(loBound) = vec(hiSwap)
    vec(hiSwap) = pivot

  '== Recursively call function .. the beauty of Quicksort
    '== 2 or more items in first section
    if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1)
    '== 2 or more items in second section
    if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound)

End Sub

The quick sort I took from the answer of this question: Sorting an Array of times in Classic ASP

Note: I cannot use System.Collections.ArrayList or other deprecated collections for the solution.

user692942
  • 16,398
  • 7
  • 76
  • 175
nyagolova
  • 1,731
  • 2
  • 26
  • 42

0 Answers0