预览模式: 普通 | 列表

asp 二维数组排序/一维数组排序

Function Sort(ary)
   Dim KeepChecking,I,FirstValue,SecondValue
   IF Not IsArray(ary) Then Exit Function
   KeepChecking = True
   Do Until KeepChecking = False
     KeepChecking = False
     For I = 0 To UBound(ary)
       IF I = UBound(ary) Then Exit For
       IF ary(I) > ary(I + 1) Then
         FirstValue = ary(I)
         SecondValue = ary(I+1)
         ary(I) = SecondValue
         ary(I+1) = FirstValue
         KeepChecking = True
       End IF
     Next
   Loop
   Sort = ary
End Function
 
二维:
 
<%
 
Function Sort(arr,u)
   Dim UNum1,UNum2
   Dim UTrue,A1,A2
   IF Not IsArray(arr) Then Exit Function
   IF Not IsNumeric(u) Then u = 0
   UNum1 = UBound(arr)
   UNum2 = UBound(arr,2)
   ReDim Arr1(UNum1),Arr2(UNum1),Arr3(UNum1,UNum2)
   For i = 0 To UNum1
     Arr1(i) = arr(i,u)
     Arr2(i) = i
   Next
   UTrue = True
   Do Until Not UTrue
     UTrue = False
     For i = 0 To UNum1
       IF i = UNum1 Then Exit For
       IF Arr1(i) > Arr1(i+1) Then
         A1 = Arr1(i):Arr1(i) = Arr1(i+1):Arr1(i+1) = A1
         A2 = Arr2(i):Arr2(i) = Arr2(i+1):Arr2(i+1) = A2
         UTrue = True
       End IF
     Next
   Loop
   For i = 0 To UNum2
     For n = 0 To UNum1
       Arr3(n,i) = arr(Arr2(n),i)
     Next
   Next
   Sort = Arr3
End Function
 
'应用实例
dim Myarray(15,9)
Randomize
response.write "<table cellSpacing=0 cellPadding=4 border=1>"
for i = 0 to UBound(Myarray,2)
   response.write "<tr>"
   for n = 0 to UBound(Myarray)
     Myarray(n,i) = fix(n*Rnd * 100 + 50 * Rnd * 2)
     response.write "<td width=35>" & Myarray(n,i) & "</td>"
   next
   response.write "</tr>" & vbnewline
next
response.write "</table><br>"
 
v = Sort(Myarray,0)
response.write "<table cellSpacing=0 cellPadding=4 border=1>"
for i = 0 to UBound(v,2)
   response.write "<tr>"
   for n = 0 to UBound(v)
     response.write "<td width=35>" & v(n,i) & "</td>"
   next
   response.write "</tr>" & vbnewline
next
response.write "</table>"
 
%>
 
<%
Function SortRev(ary) '一维的反向排序,移山补充。
   Dim KeepChecking,I,FirstValue,SecondValue
   IF Not IsArray(ary) Then Exit Function
   KeepChecking = True
   Do Until KeepChecking = False
     KeepChecking = False
     For I = 0 To UBound(ary)
       IF I = UBound(ary) Then Exit For
       IF ary(I) < ary(I + 1) Then
         FirstValue = ary(I)
         SecondValue = ary(I+1)
         ary(I) = SecondValue
         ary(I+1) = FirstValue
         KeepChecking = True
       End IF
     Next
   Loop
   SortRev = ary
End Function
dim myarr(4)
myarr(0) = 68
myarr(1) = 98
myarr(2) = 68
myarr(3) = 68
myarr(4) = 45
newarr = SortRev(myarr)
for i = 0 to ubound(newarr)
w newarr(i)
%>
分类:ASP | 固定链接 | 评论: 880 | 引用: 0 | 查看次数: 16485