2014-02-24 128 views
1

所以我有一個2d數組,我想排序。一維時我可以很容易地排序。 我希望你能幫助我。排序二維數組asp經典

這是我的數據。

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

我可以排序它一維時沒有問題。 我正在使用此代碼的一維。

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 

我想要的結果就是這個。

VerifyingInformation 5 
Calibration 4 
Greeting 2 
+0

它是正確的,你想根據自己的相關數值,把文本值按降序排列? – nvuono

+0

是的,先生,這是我想要做的。 –

回答

1

它看起來像你實際上只是湊湊熱鬧執行與相關的文本字符串數值的一維排序。

您的示例代碼很接近,但您需要2個臨時值來表示您將要移動的數組值。

 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 
+0

謝謝先生。有用。 :) –

1

這工作對我

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