SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy howto sort multi dimension array?

    can someone help sort this array..

    code:
    Code:
    var array0=new Array();
    var array1=new Array();
    var finalarray=new Array()
    					
    for(var i = 0; i < options.length ; i++){						
        array0[i]=options[i].firstChild.data;
        var id = guest_id[i].firstChild.data;
        var ip = guest_ip[i].firstChild.data;
        var array1=[id,ip];
        finalarray[i]=array1;
    }
    Sample data:
    Code:
    var array0=new Array();
    var array1=new Array();
    var finalarray=new Array()
    					
    for(var i = 0; i < 3 ; i++){						
        array0[i]=options[i].firstChild.data;
        var id = guest_id[i].firstChild.data;
        var ip = guest_ip[i].firstChild.data;
        var array1=[id,ip];
        finalarray[i]=array1;
    }
    
    
    -----------------------------
    array0 = ['a','b','c','d']
    guest_id = ['5','2','9','7']
    guest_ip = ['192.168.1.1','192.168.2.2','192.168.1.3','192.168.1.4']
    
    
    after "array1=[id,ip];"
    finalarray[] = ['5','2','9','7'] ['192.168.1.1','192.168.2.2','192.168.1.3','192.168.1.4'];
    now how do i sort guest_id in finalarray?

    this what ive done. it didnt work.

    Code:
    Attempt 1
    
    for(i = 0 ; i < finalarray.length; i++ )
    {
       var idsort = finalarray[i];
       idsort.sort(compare);
       finalarray[i] = idsort;
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Here, try it this way. The for...each loop goes through them in the guest_id order.

    Code javascript:
    guest_id = ['5','2','9','7'];
    guest_ip = ['192.168.1.1','192.168.2.2','192.168.1.3','192.168.1.4'];
     
    var finalarray = [];
    var i;
    for (i = 0; i < guest_id.length; i += 1) {
    	finalarray[guest_id[i]] = guest_ip[i];
    }
    for (var id in finalarray) {
    	alert(id);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •