ASP
  Home arrow ASP arrow Page 2 - How to Sort a Multi-Dimensional Array
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

How to Sort a Multi-Dimensional Array
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 53
    2004-06-09

    Table of Contents:
  • How to Sort a Multi-Dimensional Array
  • A Quick Explanation of the Multi-Dimensional Array Function
  • And We Proceed with Our Checking
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    How to Sort a Multi-Dimensional Array - A Quick Explanation of the Multi-Dimensional Array Function


    (Page 2 of 4 )

    The function that I'll show you is built to handle multi-dimensional arrays and sort in an ascending fashion (least to greatest). Both of these can be changed.

    The reason for handling multiple dimensions is simply that it happens many times those are the arrays we have and have to sort. Also, it's easier to handle the more complex issue of multiple dimensions in this tutorial, and let you dissect the function if you need to use it for a simple, one-dimensional array at any time.

    Also, if you feel the need to sort things in descending order (greatest to least), you could also do that after dissecting the code. After I explain the full code, I'll explain how to do this; it just doesn't make a lot of sense to do it at this point!

    Anyhow, without further delay, I will now move on to the function, what you've all come here to see.

    The Code

    Ok, so let's assume that we have an array that looks like this:

    arEvents(0, 0) = 1
    arEvents(1, 0) = "01-12-2004"
    arEvents(2, 0) = "Event #1"
    arEvents(0, 0) = 2
    arEvents(1, 0) = "10-16-2004"
    arEvents(2, 0) = "Event #2"
    arEvents(0, 0) = 3
    arEvents(1, 0) = "02-13-2004"
    arEvents(2, 0) = "Event #3"

    It could be sorted by simply handing it off to the arraySort() function, specifying which dimension to sort it by (in this case the date).

    ArEvents = arraySort( arEvents, 1, true )

    And then here's the function that's responsible for it all.

    '==================================================
    function arraySort( arToSort, sortBy, compareDates )
    '==================================================

    Dim c, d, e, smallestValue, smallestIndex, tempValue

    For c = 0 To uBound( arToSort, 2 ) - 1

    smallestValue = arToSort( sortBy, c )
    smallestIndex = c

    What's happening here is that we're going through the array one item at a time. With each item, we'll use the sortBy parameter, find the current item, and set the smallest value and index placeholders to the current item. Then we take these items, or placeholders, and compare them to EACH item in the array and look for a smaller value.

    For d = c + 1 To uBound( arToSort, 2 )

    if not compareDates then

    So if the compareDates flag has been set to false, the function will try to compare the values as strings using the inbuilt strComp() function of ASP. Now, don't be too worried about numbers, they will be handled perfectly with this function as well. When we use the strComp() function, it will produce a -1 if the compared string (the first parameter) is smaller, a zero if they're equal, and a 1 if it's larger/greater. In this case, as we're sorting depending on smaller values, we would then set the placeholders to this value and index, if we find that it is indeed smaller. Now you're probably beginning to see where modifications could be made to sort in a descending order.

    if strComp( arToSort( sortBy, d ), smallestValue ) < 0 Then
    smallestValue = arToSort( sortBy, d )
    smallestIndex = d
    End if
    else 

    So here we've got the code to handle dates, if the compareDates flag has been set to true. Of course we first check and ensure that we truly are dealing with dates. If not, we recurse, and come back through just comparing strings.

    More ASP Articles
    More By Justin Cook


       · You state:Ok, so let's assume that we have an array that looks like...
     

    ASP ARTICLES

    - ADO for the Beginner
    - ADO.NET 101: Data Rendering with a DataGrid ...
    - Introducing SoftArtisans OfficeWriter 3.0 En...
    - Getting Remote Files With ASP
    - The Real Basics of Functions in ASP
    - Enhancing Readability with ASP
    - Mimicking PHP's String Formatting Functions
    - Windows Server Hacks 12, 77, and 98
    - How to Sort a Multi-Dimensional Array
    - Developing an Information Management Tool wi...
    - What are Active Server Pages?
    - Getting Remote Pages with ASP
    - FTP’ing Files with ASP
    - Apply Single-Sign-On to Your Application
    - Easy Error Management





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway