1

So I have a 2d array that I want to Sort. I can sort it easily when one dimensional. I Hope you can help me guys.

This is my Data.

top5(0,0) = Greeting
top5(0,1) = 2
top5(1,0) = VerifyingInformation
top5(1,1) = 5
top5(2,0) = Calibration
top5(2,1) = 4

I can sort It no problem when one dimensional. I'm using this code for one dimensional.

For i = LBound(top5) to UBound(top5)
         For j = LBound(top5) to UBound(top5) - 1
              If top5(j,1) < top5(j + 1,1) Then
                 TempValue = top5(j + 1,1)
                 top5(j + 1,1) = top5(j,1)
                 top5(j,1) = TempValue
              End If
            next
        Next

The result I want to have is this.

VerifyingInformation 5
Calibration 4
Greeting 2
2
  • Is it correct that you are trying to put the text values in descending order based on their associated numeric value? Commented Feb 24, 2014 at 20:41
  • Yes sir, That is what I'm trying to do. Commented Feb 24, 2014 at 20:44

3 Answers 3

3

THIS WORKS FOR ME

function sort_arr_mult(byref ArrTmp, ordPlace)
    ' ordPlace - the place of the order value in the array
    ' create the new array
    Redim arrRet (Ubound(ArrTmp, 1), Ubound(ArrTmp, 2))

for j = 0 to Ubound(ArrTmp, 2)
    orderVal = ArrTmp(ordPlace, j)

    if j = 0 then ' first enter insert to first column
        for i = 0 to Ubound(ArrTmp, 1)
            arrRet(i, j) = ArrTmp(i, j)
        next
    else            
        ' check the first value if smaller or equal
        ' move the columnbs one field up
        ' at the end insert to currenct column
        for k = 0 to Ubound(arrRet, 2)
            if isEmp(arrRet(0, k)) then ' if empty fied the column                  
                for i = 0 to Ubound(arrRet, 1)
                    arrRet(i, k) = ArrTmp(i, j)
                next

                exit for
            else
                if orderVal<=arrRet(ordPlace, k) then                       
                    for x = Ubound(arrRet, 2) to k+1 step -1                            
                        for i = 0 to Ubound(arrRet, 1)
                            arrRet(i, x) = arrRet(i, x-1)
                        next
                    next

                    for i = 0 to Ubound(arrRet, 1)
                        arrRet(i, k) = ArrTmp(i, j)
                    next

                    exit for
                end if
            end if
        next ' for k = 0 to Ubound(arrRet, 2)
    end if
next

sort_arr_mult = arrRet
end function
Sign up to request clarification or add additional context in comments.

1 Comment

nice work mate! Tested in a large array. Work perfectly. I will extend it with 3rd parameter for ASC / DESC
1

It looks like you are actually performing a one-dimensional sort of the numeric value with an associated text string just along for the ride.

Your example code is close but you will need 2 temp values to represent the array values you will be shifting around.

       For i = LBound(top5) to UBound(top5)
            For j = LBound(top5) to UBound(top5) - 1
              If top5(j,1) < top5(j + 1,1) Then
                 TempValue = top5(j + 1,1)
                 TempText = top5(j + 1,0)
                 top5(j + 1,1) = top5(j,1)
                 top5(j + 1,0) = top5(j,0)
                 top5(j,1) = TempValue
                 top5(j,0) = TempText
              End If
            Next
       Next

Comments

1

Extending raam's answer with 3rd parameter as sorting direction "ASC" or "DESC"

Function sortArrayMulti(byref ArrTmp, ordPlace, so) 
''so: sortorder "ASC" or "DESC"
Dim j, i, k, orderVal, x
Redim arrRet(Ubound(ArrTmp, 1), Ubound(ArrTmp, 2))
for j = 0 To Ubound(ArrTmp, 2)
    orderVal = ArrTmp(ordPlace, j)
    if j = 0 Then
        for i = 0 to Ubound(ArrTmp, 1)
            arrRet(i, j) = ArrTmp(i, j)
        next
    else
        for k = 0 to Ubound(arrRet, 2)
            if isEmpty(arrRet(0, k)) then
                for i = 0 to Ubound(arrRet, 1)
                    arrRet(i, k) = ArrTmp(i, j)
                next
                exit for
            else
                if so = "ASC" then
                    if orderVal <= arrRet(ordPlace, k) then
                        for x = Ubound(arrRet, 2) to k + 1 step -1
                            for i = 0 to Ubound(arrRet, 1)
                                arrRet(i, x) = arrRet(i, x - 1)
                            next
                        next
                        for i = 0 to Ubound(arrRet, 1)
                            arrRet(i, k) = ArrTmp(i, j)
                        next
                        exit for
                    end if
                else
                    if orderVal >= arrRet(ordPlace, k) then
                        for x = Ubound(arrRet, 2) to k + 1 step -1
                            for i =  Ubound(arrRet, 1) to 0 step -1
                                arrRet(i, x) = arrRet(i, x - 1)
                            next
                        next
                        for i = 0 to Ubound(arrRet, 1)
                            arrRet(i, k) = ArrTmp(i, j)
                        next
                        exit for
                    end if
                end if
            end if
        next
    end if
next
sortArrayMulti = arrRet
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.