ASP
  Home arrow ASP arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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


    How to Sort a Multi-Dimensional Array - Conclusion


    (Page 4 of 4 )

    So that is the wonderful, magical function with all of its very limited glory. If you decide you want to try sorting in the reverse direction (descending), then you've probably already figured out where you need to add code. If not, I'll give you a hint: it is only a matter of passing in another parameter to the function (the direction), and a few control flow statements to set the placeholders in the array to be minimum OR maximum values. You then just change the dateDiff() and strComp() lines to look for inverse integers (greater than zero instead of less than, and vise versa), depending on the direction parameter. And there you will have it, one awesomely complete array sorter.

    Most people ask me for the code all in one piece, because they don't like gathering bits and pieces between explanations, so here's the code for the function all in one piece:

    '==============================================
    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

    For d = c + 1 To uBound( arToSort, 2 )
    if not compareDates then
    if strComp( arToSort( sortBy, d ), smallestValue ) < 0 Then
    smallestValue = arToSort( sortBy, d )
    smallestIndex = d
    End if

    else
    if not isDate( smallestValue ) then
    arraySort = arraySort( arToSort, sortBy, false)
    exit function

    else
    if dateDiff( "d", arToSort( sortBy, d ), smallestValue ) > 0 Then
    smallestValue = arToSort( sortBy, d )
    smallestIndex = d
    End if

    end if

    end if

    Next

    if smallestIndex <> c Then 'swap
    For e = 0 To uBound( arToSort, 1 )
    tempValue = arToSort( e, smallestIndex )
    arToSort( e, smallestIndex ) = arToSort( e, c )
    arToSort( e, c ) = tempValue
    Next
    End if

    Next


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 1 hosted by Hostway