ASP
  Home arrow ASP arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 58
    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


    How to Sort a Multi-Dimensional Array


    (Page 1 of 4 )

    Yes, the title is a mouthful. But unfortunately with classic ASP, trying to make sense of an unsorted array is much worse. It is quite often the case, especially when retrieving data from a database, that it may end up in an array which is not ordered perfectly, perhaps due to incorrect data type settings. And of course, ASP would provide us with no intrinsic array sorting mechanisms. This article will explain how to take the array of that data, and hand it off to a function to sort it based on whichever dimension you specify, also with the ability to handle various data types.

    Introduction

    I don't especially enjoy sorting laundry. It's boring, and feels somewhat pointless, but at least it's easy to do. It would not be nearly as easy for a web application to sort your laundry. Though it would be far more willing to do work, it just wouldn't understand what laundry is, why it needs to be sorted, and in what order. And as it turns out, if you're using ASP, an array of any type may as well be laundry, because there's no intrinsic way to sort arrays, no matter how simple it may be.

    The first time I ran across this issue was with an array of dates. The dates were for events, and all the event information was stored in a database. Normally it wouldn't be an issue retrieving the dates in a properly sorted result set; I could just specify an ORDER BY clause in the SQL statement, which I tried. But the problem here was that the table the events were in was exceedingly normalized. Instead of having separate fields for the date, event title, and other attributes, I had one field called 'content' and another field to identify the type of content it is. That means that every field, every date, every time was stored as the generic type of text.

    So I would go about retrieving the data, trying to order it properly, and it worked well for the most part. But when you treat a date as text, funny things happen. Though you and I know that October is farther away than February, a database holding the values as text does not. You see, October would be '10-01-2004', and February would be '02-01-2004', which looks like a larger text value than the first, and when you dump the results into the array you get one funky calendar.

    As you can guess, people did not derive extreme pleasure in the application telling them that events in October were now happening at the beginning of the year, so something needed to be done. I had dumped all the data for the events into a multi-dimensional array, so the first thing I did was consult some ASP documentation on how to sort arrays. Much to my chagrin there was nothing to be found. So, I was forced to go it alone, hack out some method to do my sorting, the results of which I share with you today.

    More ASP Articles
    More By Justin Cook


       · You state:Ok, so let's assume that we have an array that looks like...
       · Thank you for sharing your function, it has been very useful to me and saved me...
     

    ASP ARTICLES

    - Using MySQL with ASP
    - 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





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek