Home >Web Front-end >JS Tutorial >More correct asp bubble sort_javascript skills
The code found on the Internet is always this
Function Sort(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
Sort = ary
End Function
There is an error. . . . . .
You will know after testing it
s="11,3,1"
s=sort(split(s,","))
for i=0 to ubound (s)
response.write s(i) & "
"
next
The print result is
1
11
3
The correct function is:
function sort(ary)
ck=true
do Until ck = false
ck=false
For f = 0 to UBound(ary) -1
if clng(ary(f))>clng(ary(f 1)) then
v1=clng(ary(f))
v2=clng(ary(f 1 ))
ary(f)=v2
ary(f 1)=v1
ck=true
end if
next
loop
sort=ary
end function
The only difference is clng()
But the funny thing is that some arrays can be sorted correctly using the wrong sort function.