Results 1 to 9 of 9

Thread: Sorting Multidimensional Array

  1. #1
    Member
    Join Date
    Jul 2001
    Location
    New Jersey
    Posts
    50

    Sorting Multidimensional Array

    How do I set up a compare function (sorting arrays) for a multi-dimensional array when I want the sort to be based on the first 2 elements of the array.

    If I have the array
    3,Kevin
    3,Alice
    2,Steve
    1,Adam
    2,Sammy

    I want to sort it so I get
    1,Adam
    2,Sammy
    2,Steve
    3,Alice
    3,Kevin


    array.sort(compareFunction)

    function compareFunction(???)
    { ???? }


    Any Help in the Compare Function would be appreciated. Or if there is a better approach.
    I am only concerned with IE 4.0+

  2. #2
    Moderator
    Join Date
    Oct 2000
    Location
    Should be used instead of document.location . Except with a small l.
    Posts
    3,224
    Hi Kevin,

    You should write a function that accepts two arguments (a and b) and should return something less than 0 if a < b, 0 if a == b or something greater than 0 if a > b . So you should write something comparing a[0] and b[0] and if they are equal compare a[1] and b[1].
    Form Parser (pass values from one page to another):
    FAQ Entry | Original Thread | Having Problems with the script? | Put Values Back Into Another Form

    Navigation: Frame

  3. #3
    Member
    Join Date
    Jul 2001
    Location
    New Jersey
    Posts
    50
    I think I understand, Thanks

  4. #4
    Moderator
    Canada Herb's Avatar
    Join Date
    Dec 1999
    Location
    Saarbrücken, Germany
    Posts
    1,478
    Hi,

    I did something that will do the job:
    Code:
    function compare ( left, right ) {
        
        if ( left[0] < right[0] )
    	return 1;
        else if ( left[0] > right[0] )
    	return -1;
        else if ( left[1] < right[1] )
    	return 1;
        else if ( left[1] > right[1] )
    	return -1;
        else
    	return 0;
        
    }
    
    
    function qsort ( array, lo, hi ) {
    
      var low  = lo;
      var high = hi;
      mid = array[ Math.floor( (low+high)/2 ) ];
    
      do {
        while ( compare(array[low],  mid) > 0 )
          low++;
    
        while ( compare(array[high], mid) < 0 )
          high--;
    
        if ( low <= high ) {
          swap( array, low, high );
          low++;
          high--;
        }
    
      } while ( low <= high );
    
      if ( high > lo )
        qsort( array, lo, high );
    
      if ( low < hi )
        qsort( array, low, hi );
    
    }
    
    function swap ( a, i, j ) {
    
      var tmp = a[i]; 
      a[i] = a[j];
      a[j] = tmp;
    
    }
    
    
    var array = new Array();
    
    array[0] = new Array ( 3, "Kevin" );
    array[1] = new Array ( 3, "Alice" );
    array[2] = new Array ( 2, "Steve" );
    array[3] = new Array ( 1, "Adam"  );
    array[4] = new Array ( 2, "Sammy" );
    
    qsort ( array, 0, array.length-1 );
    It uses an adapted version of quicksort, the fastest sorting algorithm.

    See the results at http://www.iro.umontreal.ca/~saintamh/test/sort

  5. #5
    Moderator
    United Nations Vincent Puglia's Avatar
    Join Date
    Jan 2000
    Location
    where the World once stood
    Posts
    4,961
    Hi theScum

    mon dieu, c'est manifique!

    Only took a glance, but she does shine.

    Vinny
    The Blades of Grass cut me still
    grassBlade

  6. #6
    Moderator
    Canada Herb's Avatar
    Join Date
    Dec 1999
    Location
    Saarbrücken, Germany
    Posts
    1,478
    mon dieu, c'est manifique!
    hahahahaha

    merci

  7. #7
    Moderator
    United Nations Vincent Puglia's Avatar
    Join Date
    Jan 2000
    Location
    where the World once stood
    Posts
    4,961
    hi theScum,

    laughing at my french ?

    post it in the "Post a Javascript" section with some explanation.

    thanks
    Vinny
    The Blades of Grass cut me still
    grassBlade

  8. #8
    Moderator
    Canada Herb's Avatar
    Join Date
    Dec 1999
    Location
    Saarbrücken, Germany
    Posts
    1,478
    Hi Vinny and others,

    I was not laughing at your french, it's just that the sentence was maybe a bit exaggerated

    I wrote the explanations, and also improved the script so that it can sort arrays with more than 2 columns. Check it out.

    ciao,
    Scum

  9. #9
    Moderator
    Canada Herb's Avatar
    Join Date
    Dec 1999
    Location
    Saarbrücken, Germany
    Posts
    1,478
    I just fixed a bug in the code that would make an error if two rows were equal.

    I also added simplified instructions for those who want to use it but are not interested in how it works...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. JavaScript Multidimensional Array Problem
    By MamaGeek in forum Client-side forum
    Replies: 4
    Last Post: 11th May 2005, 18:33
  2. Array Sorting - Not your usual sorting!
    By Carl V in forum Server-side Forum
    Replies: 1
    Last Post: 7th February 2005, 00:42
  3. array sorting
    By patriko2000 in forum Server-side Forum
    Replies: 1
    Last Post: 17th October 2001, 11:09
  4. Perl Array Sorting 201?
    By Snover in forum Server-side Forum
    Replies: 2
    Last Post: 25th April 2001, 15:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •