日期:2014-05-17  浏览次数:20923 次

问个数组排序问题!很纠结
数组如下:


Dim arr 
arr = Array("道_1","道_2","道_113","道_14","道_21","道_132","道_31","道_15","道_34","道_25")

现在要 按照数字大小来排序 排了半天没效果

VB code
<%
'排序 
Function Sort1(ary) 
Dim KeepChecking,I,FirstValue,SecondValue 
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 
Sort1 = ary 
End Function 

Dim arr 
arr = Array("道_1","道_2","道_113","道_14","道_21","道_132","道_31","道_15","道_34","道_25")

arr = Sort1(arr) 
For i=0 to ubound(arr) 
Response.Write(arr(i)&"<br />") 
Next 


%>


上面代码显示为:

道_1
道_113
道_132
道_14
道_15
道_2
道_21
道_25
道_31
道_34


前面是1 的在前面了。没按大小来!有什么解决办法 


------解决方案--------------------
VBScript code

<%
Dim arr 
arr = Array("道_1","道_2","道_113","道_14","道_21","道_132","道_31","道_15","道_34","道_25")
Dim rs, i, a
Set rs = CreateObject("ADODB.RecordSet")
rs.Fields.Append "pre",200,50
rs.Fields.Append "num",3,4
rs.CursorLocation = 3
rs.Open
For i = 0 To UBound(arr)
    a = Split(arr(i), "_")
    rs.AddNew
    rs("pre") = a(0)
    rs("num") = a(1)
Next
rs.Sort = "num ASC"
rs.MoveFirst
i = 0
Do While Not rs.EOF
    arr(i) = rs("pre") & "_" & rs("num")
    i = i + 1
    rs.MoveNext
Loop
rs.Close
Set rs = Nothing

Response.Write Join(arr, ",")  
%>