Sort and array: ascending or descending order

In this examples we will show how to order values in a array.

The only diference between arrays bellow is indicated in bold:

  • to get ascending values,  if MyArray(i)>MyArray(j), values will be replace by each other.
  • to get descending values,  if MyArray(i)<MyArray(j),  values will be replace by each other.
When comparing values, i will be always greater or equal to j.

Scripts will work both for numbers or string.
 


Sort: Ascending

<%
MyArray=Array(25,14,20,45,25,4,1,31,22,7)

max=ubound(MyArray)

For i=0 to max 
   For j=i+1 to max 
if MyArray(i)>MyArray(j) then
TemporalVariable=MyArray(i)
MyArray(i)=MyArray(j)
MyArray(j)=TemporalVariable
     end if
   next 
next 

Response.write ("The sorted values are those ones: <BR>") 

For i=0 to max 
  Response.write (MyArray(i) & "<BR>") 
next 
%>

The sorted values are these ones: 
1
4
7
14
20
22
25
25
31
45


Sort: Descending

<%
MyArray=Array("John","Peter","Anna","Alberto","George",
"Unai")

max=ubound(MyArray)

For i=0 to max
For j=i+1 to max
if MyArray(i)<MyArray(j) then
TemporalVariable=MyArray(i)
MyArray(i)=MyArray(j)
MyArray(j)=TemporalVariable
end if
next
next

Response.write ("The sorted values are these ones: <BR>")

For i=0 to max
Response.write (MyArray(i) & "<BR>")
next
%>

The sorted values are these ones:
Unai
Peter
John
George
Anna
Alberto



Google






   



MSN Nick Name



More Resources...





Most Viewed Services:
  1. HTML Tutorial
  2. XHTML Tutorial
  3. CSS Tutorial
  4. Javascript Tutorial
  5. DHTML Tutorial
  6. VB Script
  7. TCP/IP Tutorial
  8. ADO Tutorial
  9. MYSQL Tutorial
  10. ASP Tutorial
  11. AJAX Tutorial
  12. CFML Tutorial
  13. PHP Tutorial
  14. WML Tutorial
  15. FLASH Tutorial
  16. XML Tutorial
  17. RSS Tutorial
  18. SQL Tutorial
  19. HTML Articles
  1. Javascript Articles
  2. PHP Articles
  3. SEO Articles
  4. Web Design Articles
  5. SEO Tips
  6. Web Design Tips
  7. Articles
  8. CSS
  9. CSS Tips
  10. HTML Tips
  11. JAVASCRIPT Tips
  12. MYSQL Tips
  13. PHP Tips
  14. Money
  15. Tutorials
  16. Web Hosting



  • Home
  • Web Directory
  • Top Directoriers
  • Webmaster Directories
  • Contact
  • © Copyright 2006-2010 All Rights Reserved By CodeDcode.Com : HTML : RSS : TEXT : XML