x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 321

# sort array in descending order

Hello Experts,

I'm struggling to find a workaround to sort a numeric array in  descending order. Here is what I could compile from searching the web:

<%

iArray = ("62|37|37|100|37|37|37|37|100|37")
percArray = split(iArray,"|")
max = UBound(percArray)

For iLoop = 0 to max
For j = iLoop + 1 to max
if percArray(iLoop) > percArray(j) then
TempVar = percArray(iLoop)
percArray(iLoop) = percArray(j)
percArray(j) = TempVar
end if
next
next

For iLoop = 0 to max
Response.Write percArray(iLoop) & "<br>"
next

%>

For some reason this isn't working, I tried percArray(iLoop) > percArray(j) AND percArray(iLoop) < percArray(j). Still the numbers don't get sorted in descending order. Can anyone suggest or maybe correct this function ?

thanks
0
pazan
• 2
1 Solution

Systems and Integration DeveloperCommented:
Try:

<%

iArray = ("62|37|37|100|37|37|37|37|100|37")
percArray = split(iArray,"|")
max = UBound(percArray)

For iLoop = 0 to max
For j = iLoop + 1 to max
if CInt(percArray(iLoop)) < CInt(percArray(j)) then
TempVar = percArray(iLoop)
percArray(iLoop) = percArray(j)
percArray(j) = TempVar
end if
next
next

For iLoop = 0 to max
Response.Write percArray(iLoop) & "<br>"
next

%>
0

Commented:
This should be exactly what you want:

http://www.evolt.org/node/21724
0

Commented:
QuickSort is a quick and reliable sorting algorithm, but if you have a small array I would probably recommend insertion sort: http://www.freevbcode.com/ShowCode.asp?ID=157
0

Author Commented:
strickdd , I read that page(evolt) earlier. There's a huge script, maybe one day if I will have some problems with my existing script I will migrate to Quicksort.

carl_tawn, that works nicely, Cint did the job :)

thanks, I will give points to carl_tawn, since he corrected existed script.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.