asp下的数组排序,一维和二维例子

[ 2007-10-06 13:09:10 | 作者: admin ]
字号: | |
一维
引用
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>"
http://www.9ba.cn/post/347.html
[最后修改由 admin, 于 2007-10-06 13:13:16]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=999

这篇日志没有评论。

此日志不可发表评论。