Ordering when mergin arrays with jquery
<p>What I'd like to know is how to merge the Introduction, matchArray, and segmentArray into the subArray however there is certain conditions on which the ordering needs to be achieved. The Introduction will always be put first in the subArray. After the Introduction it will place each of the matches from the matchArray into the subArray. Then for each of the segments inside of the segmentArray it will look at each and its segmentOrder value because that will tell where that segment should be placed inside the subArray. If the first segment's segmentOrder value says "introduction" then that segment will be placed after the Introduction. If it says 1 for the segmentOrder then it gets placed after Match 1. If there are two segments that have the same value for the segmentOrder than it orders those two by the segmentNumber.</p>

<p>This script works and dumps the following:</p>

<pre><code>   '0' ...
   'type' ...
   '0' =&gt; "Match"
   '1' =&gt; "Match"
   'matchNum' ...
   '0' =&gt; "1"
   '1' =&gt; "2"
   'matchtypeID' ...
   '0' =&gt; "1"
   '1' =&gt; "1"
   'titlesIDList' ...
   '0' =&gt; "3,2"
   '1' =&gt; "3,2"
   'stipulationsIDList' ...
   '0' =&gt; "2,1"
   '1' =&gt; "2,1"
   'competitorsIDList' ...
   '0' =&gt; "10,3"
   '1' =&gt; "11,1"
   'matchWriterID' ...
   '0' =&gt; "1"
   '1' =&gt; "1"
   'matchTitle' ...
   '0' =&gt; "match title1"
   '1' =&gt; "match title2"
   'preview' ...
   '0' =&gt; "preview1"
   '1' =&gt; "preview2"


   '0' ...
   'type' ...
   '0' =&gt; "Segment"
   '1' =&gt; "Segment"
   'segmentNumber' ...
   '0' =&gt; "1"
   '1' =&gt; "2"
   'segmentWriterID' ...
   '0' =&gt; "1"
   '1' =&gt; "1"
   'segmentOrder' ...
   '0' =&gt; "introduction"
   '1' =&gt; "1"
</code></pre>

<p>My most recent code:</p>

<pre><code>                    
var introduction = $("#introduction").val();
                    var subArray = new Array;
                    var matchArray = new Array;
                    var matchArrayIndex = 0;
                    var segmentArray = new Array;
                    var segmentArrayIndex = 0;
                    var matchNumX = new Array;
                    var matchTypeIDX = new Array;
                    var titlesIDListX = new Array;
                    var stipulationsIDListX = new Array;
                    var competitorsIDListX = new Array;
                    var matchWriterIDX = new Array;
                    var matchTitleX = new Array;
                    var previewX = new Array; 
                    var segmentNumX = new Array;
                    var segmentWriterIDX = new Array;
                    var segmentOrderX = new Array;
                    var typeX = new Array;
                    var typeX2 = new Array;
                    var i = 0;

                    for(i=0;i; i++) {

                        var matchNum = i + 1;
                        var matchTypeID = $('select#matchTypeDrop'+ matchNum).val();  
                        var liTitles = $('ul#titlesDefended'+ matchNum +' li');
                        var titlesIDList = "";
                        var j = 0;
                        $('ul#titlesDefended'+ matchNum +' li').each(function(){
                            var liTitle = $( liTitles[ j ] );

                            // only start appending commas in after the first characterID
                            if( j > 0 ) {
                                titlesIDList += ","; 
                            }

                            // append the current li element's characterID to the list
                            titlesIDList += liTitle.attr( 'titleID' );
                            j++;
                        });
                        var j = 0;
                        var liStipulations = $('ul#stipulationsAdded'+ matchNum +' li');
                        //alert(liStipulations);
                        var stipulationsIDList = "";
                        $('ul#stipulationsAdded'+ matchNum +' li').each(function(){
                            var liStipulation = $( liStipulations[ j ] );
                            //alert(liStipulation);

                            // only start appending commas in after the first characterID
                            if( j > 0 ) {
                                stipulationsIDList += ","; 
                            }

                            // append the current li element's characterID to the list
                            stipulationsIDList += liStipulation.attr( 'stipulationID' );
                            //alert(liStipulation.attr( 'stipulationID' ));
                            j++;
                        });
                        //alert(stipulationsIDList);
                        var j = 0;
                        var liCompetitors = $('ul#competitors'+ matchNum +' li');
                        var competitorsIDList = "";
                        $('ul#competitors'+ matchNum +' li').each(function(){
                            var liCompetitor = $( liCompetitors[ j ] );

                            // only start appending commas in after the first characterID
                            if( j > 0 ) {
                                competitorsIDList += ","; 
                            }

                            // append the current li element's characterID to the list
                            competitorsIDList += liCompetitor.attr( 'competitorID' );
                            j++;
                        });
                        var matchWriterID = $('select#matchWriter' + matchNum + '').val();
                        var matchTitle = $('input#matchTitle'+ matchNum +'').val();
                        var preview = $('textarea#preview'+ matchNum +'').val();
                        var type = $("input#type").val();
                        typeX.push(type);
                        matchNumX.push(matchNum);
                        matchTypeIDX.push(matchTypeID);
                        titlesIDListX.push(titlesIDList);
                        stipulationsIDListX.push(stipulationsIDList);
                        competitorsIDListX.push(competitorsIDList);
                        matchWriterIDX.push(matchWriterID);
                        matchTitleX.push(matchTitle);
                        previewX.push(preview);
                        matchArray[matchArrayIndex] = new Object();
                        matchArray[matchArrayIndex]['type'] = typeX;               
                        matchArray[matchArrayIndex]['matchNum'] = matchNumX;
                        matchArray[matchArrayIndex]['matchtypeID'] = matchTypeIDX;
                        matchArray[matchArrayIndex]['titlesIDList'] = titlesIDListX;
                        matchArray[matchArrayIndex]['stipulationsIDList'] = stipulationsIDListX;
                        matchArray[matchArrayIndex]['competitorsIDList'] = competitorsIDListX;
                        matchArray[matchArrayIndex]['matchWriterID'] = matchWriterIDX;
                        matchArray[matchArrayIndex]['matchTitle'] = matchTitleX;
                        matchArray[matchArrayIndex]['preview'] = previewX;
                    }
                    for(i=0;i; i++) {
                        var segmentNum = i + 1;
                        var segmentWriterID = $('select#segmentWriter' + segmentNum + '').val();
                        var segmentOrder = $('select#segmentOrder' + segmentNum + '').val();
                        var type = $("input#type2").val();
                        typeX2.push(type);
                        segmentNumX.push(segmentNum);
                        segmentWriterIDX.push(segmentWriterID);
                        segmentOrderX.push(segmentOrder);
                        segmentArray[segmentArrayIndex] = new Object();     
                        segmentArray[segmentArrayIndex]['type'] = typeX2;          
                        segmentArray[segmentArrayIndex]['segmentNumber'] = segmentNumX;
                        segmentArray[segmentArrayIndex]['segmentWriterID'] = segmentWriterIDX;
                        segmentArray[segmentArrayIndex]['segmentOrder'] = segmentOrderX;
                    }
                    alert(dump(matchArray));
                    alert(dump(segmentArray));
                    var dataString = 'masterArray=' + masterArray;
                    dataString += '&eventID=' + eventID + '&submitBooking=True';  
  </code></pre>
