Solved

sort array in descending order

Posted on 2006-11-29
4
305 Views
Last Modified: 2012-06-21
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
Comment
Question by:pazan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 300 total points
ID: 18037130
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
 
LVL 28

Expert Comment

by:strickdd
ID: 18037150
This should be exactly what you want:

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

Expert Comment

by:strickdd
ID: 18037166
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 Comment

by:pazan
ID: 18037217
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

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question