1

I have a 2d array and would like to sort each row based on the final column. I have created a mergesort algorithm in VBScript (which is what I am going to use) that does the sorting for a single column. But I would like to sort every row based on the final column like this, where I want the rows to be sorted based on the last row.

Name | X value | Y value | Z value

R1 | 10 | 3 | 2

There is a code mentioned in this post that does sorting for single dimension array sorting (https://stackoverflow.com/a/10351062/17862830). I have tried editing this code to solve my problem by extracting the integer that I am comparing from the strings in the array (extracting 19 from "Name,0,0,0,0,19"). However, I am not sure why does the code not work well as compared to doing it as just pure integer.

'The merge function.
Public Function Merge(LeftArray, RightArray, Order)
    'Declared variables
    Dim FinalArray
    Dim FinalArraySize
    Dim i
    Dim LArrayPosition
    Dim RArrayPosition

    'Variable initialization
    LArrayPosition = 0
    RArrayPosition = 0

    'Calculate the expected size of the array based on the two smaller arrays.
FinalArraySize = UBound(LeftArray) + UBound(RightArray) + 1
ReDim FinalArray(FinalArraySize)

'This should go until we need to exit the function.
While True

    'If we are done with all the values in the left array.  Add the rest of the right array
    'to the final array.
    If LArrayPosition >= UBound(LeftArray)+1 Then
        For i=RArrayPosition To UBound(RightArray)
            FinalArray(LArrayPosition+i) = RightArray(i)
        Next
        Merge = FinalArray
        Exit Function

    'If we are done with all the values in the right array.  Add the rest of the left array
    'to the final array.
    ElseIf RArrayPosition >= UBound(RightArray)+1 Then
        For i=LArrayPosition To UBound(LeftArray)
            FinalArray(i+RArrayPosition) = LeftArray(i)
        Next
        Merge = FinalArray
        Exit Function

    'For descending, if the current value of the left array is greater than the right array 
    'then add it to the final array.  The position of the left array will then be incremented
    'by one.
    ElseIf getNumber(LeftArray(LArrayPosition)) > getNumber(RightArray(RArrayPosition)) And UCase(Order) = "DESC" Then'**
        FinalArray(LArrayPosition+RArrayPosition) = LeftArray(LArrayPosition)
        LArrayPosition = LArrayPosition + 1

    'For ascending, if the current value of the left array is less than the right array 
    'then add it to the final array.  The position of the left array will then be incremented
    'by one.
    ElseIf getNumber(LeftArray(LArrayPosition)) < getNumber(RightArray(RArrayPosition)) And UCase(Order) = "ASC" Then'**
        FinalArray(LArrayPosition+RArrayPosition) = LeftArray(LArrayPosition)
        LArrayPosition = LArrayPosition + 1

    'For anything else that wasn't covered, add the current value of the right array to the
    'final array.
    Else
        FinalArray(LArrayPosition+RArrayPosition) = RightArray(RArrayPosition)
        RArrayPosition = RArrayPosition + 1
    End If
Wend
End Function

'The main sort function.
Public Function Sort(ArrayToSort, Order)
'Variable declaration.
Dim i
Dim LeftArray
Dim Modifier
Dim RightArray

'Check to make sure the order parameter is okay.
If Not UCase(Order)="ASC" And Not UCase(Order)="DESC" Then
    Exit Function
End If
'If the array is a singleton or 0 then it is sorted.
If UBound(ArrayToSort) <= 0 Then
    Sort = ArrayToSort
    Exit Function
End If

'Setting up the modifier to help us split the array effectively since the round
'functions aren't helpful in VBScript.
If UBound(ArrayToSort) Mod 2 = 0 Then
    Modifier = 1
Else
    Modifier = 0
End If

'Setup the arrays to about half the size of the main array.
ReDim LeftArray(Fix(UBound(ArrayToSort)/2))
ReDim RightArray(Fix(UBound(ArrayToSort)/2)-Modifier)

'Add the first half of the values to one array.
For i=0 To UBound(LeftArray)
    LeftArray(i) = ArrayToSort(i)
Next

'Add the other half of the values to the other array.
For i=0 To UBound(RightArray)
    RightArray(i) = ArrayToSort(i+Fix(UBound(ArrayToSort)/2)+1)
Next

'Merge the sorted arrays.
Sort = Merge(Sort(LeftArray, Order), Sort(RightArray, Order), Order)
End Function

Dim arr
arr = Array("R1,0,0,0,0,12","R1,0,0,0,0,1","R1,0,0,0,0,2","R1,0,0,0,0,124", "R1,0,0,0,0,150","R1,0,0,0,0,9756","R1,0,0,0,0,200","R1,0,0,0,0,14","R1,0,0,0,0,-124","R1,0,0,0,0,-12","R1,0,0,0,0,0")
Dim sortarr : sortarr = Sort(arr, "asc")
Dim secsortarr : secsortarr = Sort(sortarr, "asc")
For i=0 To UBound(secsortarr)
    MsgBox(secsortarr(i))
Next

Function getNumber(row)
    Dim holdarr
    holdarr = Split(row, ",")
    getnumber = holdarr(UBound(holdarr))
End Function
  • Does this answer your question? [Sort multidimensional array in VBScript](https://stackoverflow.com/questions/12282392/sort-multidimensional-array-in-vbscript) – user692942 Oct 20 '22 at 06:25
  • @user692942 Hi, I feel like that code will not suit my requirement as I require one that can does the sorting rather quickly. I have tried adapting a mergesort code to sort the last integers in a string, but the code is not really working. May I know if you could help me with it? –  Oct 20 '22 at 06:45
  • @user692942 Just wanted to know, can a disconnected recordset handle large amounts of elements? –  Oct 20 '22 at 08:42
  • @user692942 I am writing this script to run on an application that only uses VBScript, thus I kind of have no choice but to use VBScript –  Oct 20 '22 at 09:18
  • 2
    Replacing `getnumber = holdarr(UBound(holdarr))` with `getnumber = CInt(holdarr(UBound(holdarr)))` should do what you want – Flakes Oct 20 '22 at 10:37
  • 2
    @Flakes It works now with that edit. Thanks! –  Oct 21 '22 at 00:51

0 Answers0