ASP 订阅所有【ASP】的日志

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)
%>


[本日志由 李超 于 2014-03-06 03:10 PM 更新]
上一篇: chrome下flash插件长期狂飙CPU怎么办?
下一篇: 如何跻身独角兽俱乐部:11条建议帮你创建十亿美元级互联网企业
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 880 | 引用: 0 | 查看次数: 15594
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 关闭 | [img]标签 关闭