Robb Meade wrote: > "Ray at <%=sLocation%>" wrote ... > >> Note that these fields will not be returned in any controllable >> order. > > Cheers Ray thats grand... > > Know of anyway to sort an array? :) > > Robb One way is to use a bubble sort, which will give acceptable performance on small (>100) data sets. dim ar, i , j, vTemp ar=array(dog,cat,bird,monkey) response.write "Before Sort:<BR>" for i = 0 to ubound(ar) response.write ar(i) & "<BR>" next for i=0 to ubound(ar)-1 for j = i+1 to ubound(ar) if ar(j) <= ar(i) then vTemp = ar(j) ...
Robb Meade wrote:One way is to use a bubble sort, which will give acceptable performance on> "Ray at <%=sLocation%>" wrote ...
>>>> Note that these fields will not be returned in any controllable
>> order.
> Cheers Ray thats grand...
>
> Know of anyway to sort an array? :)
>
> Robb
small (>100) data sets.
dim ar, i , j, vTemp
ar=array(dog,cat,bird,monkey)
response.write "Before Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next
for i=0 to ubound(ar)-1
for j = i+1 to ubound(ar)
if ar(j) <= ar(i) then
vTemp = ar(j)
ar(j) = ar(i)
ar(i) = vTemp
end if
next
next
response.write "After Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next
To sort numbers, change "if ar(j) <= ar(i) then" to "if cdbl(ar(j)) <=
cdbl(ar(i)) then", or cint, clng, etc., depending on what type of numbers
you are sorting.
Another way is to use an ad hoc recordset:
dim ar, i , rs
ar=array(dog,cat,bird,monkey)
response.write "Before Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next
Set rs=server.createobject("adodb.recordset")
'again, use the datatype that makes sense for your data
rs.fields.append "data",adVarChar,20
rs.open
for i = 0 to ubound(ar)
rs.addnew "data", ar(i)
next
rs.sort "data"
i=0
do until rs.eof
ar(i) = rs(0)
i=i+1
loop
rs.close
set rs=nothing
response.write "After Sort:<BR>"
for i = 0 to ubound(ar)
response.write ar(i) & "<BR>"
next
HTH,
Bob Barrows
Bookmarks