Results 1 to 4 of 4

Thread: how do you sort a combined array

  1. #1
    Join Date
    Jun 2010

    how do you sort a combined array

    This is my coding:

    var company=new Array("Kestrel Moon","BB")
    var femalePri=new Array(89,0)

    for(var i=0;i<2;i++){
    var companyTotal=company[i].concat(finalTotal[i]);

    this displays:

    Kestrel Moon 89
    BB 0

    I want it so that it displays in ascending order of number

    BB 0
    Kestrel Moon 89

    The problem is that when I do a normal sort array, it puts the company names in alphabetical order and the price stays as it is and if I sort the numbers in ascending order, the numbers go in ascending order but the company names stays where ther are.

    I want it so that numerically its in acscending order and when the price is placed in ascending order, their company names go with their prices, can this be done and if it can, can you please show me an example?

    thank you

  2. #2
    Join Date
    Dec 2005

    Question Question ...

    It can be done, but I have a question ...

    How long are you into the developmental process?
    Can the two Arrays be combined into a single array like
    // var company=new Array("Kestrel Moon","BB")
    // var femalePri=new Array(89,0)
    var companyFemale = [
         "Kestrel Moon,89",
    It could possibly simplify the task if data could be arranged this way.

    If not, the task can still be accomplished, but with a bit more code.

  3. #3
    Join Date
    Oct 2006
    HTML Code:
    <SCRIPT type="text/javascript">
    //Concat the 2 arrays with the number going first, name second. Then, sort by parseInt()
    	var company=new Array("Kestrel Moon","BB")
    	var femalePri=new Array(89,0)
    	var companyTotal=[]
    	for(i=0;i<2;i++){	 companyTotal[i]=femalePri[i]+'~'+company[i];}
    	companyTotal.sort(function(a,b){return parseInt(a) - parseInt(b)});
    	document.write( companyTotal.join('<br>') );
    	document.write( '<P>' );
    //each element of companyTotal can now be split into an Array in
    // order to write it with name first
    		document.write(companyTotal[i][1]+' '+companyTotal[i][0]+'<br>')}

  4. #4
    Join Date
    Dec 2005

    Lightbulb Something to consider ...

    Using 'justinbarneskin's sort function and my idea of combining the master array,
    you could do something like this (or modify further for the remaining fields) ...
    <title> Master Array Display </title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?t=197529
    //  and: http://www.codingforums.com/showthread.php?t=197530
    //  and: http://www.webdeveloper.com/forum/showthread.php?t=230780
    var masterArray = [
    	'Kestrel Moon: ,0,1165,475,97,0,98',
    	'BB: ,89,1231,380,43,180,85',
    	'Whatever You Drive: ,34,1052,210,23,450,43',
    	'Discount Drivers: ,291,1667,310,302,189,93',
    	'Indirect Curve: ,0,804,599,45,0,0',
    	'Swindel: ,187,1010,210,35,32,90',
    	'Captain: ,0,510,100,19,0,40',
    	'Saintly Drivers: ,0,890,0,0,0,155',
    	'Ipswich Union: ,0,490,0,0,0,0',
    	'Young Drivers: ,203,803,0,78,0,82',
    	'Atlee: ,107,923,200,81,92,70',
    	'Good Deal: ,434,1690,300,112,493,47',
    	'Nice Price: ,0,775,0,487,0,30',
    	'Insure For Less: ,998,1435,0,49,0,59',
    	'Instant Insure: ,43,1223,424,113,68,96' 
    // following for testing purposes only
    // document.write('Original Array<p>');
    // document.write(masterArray.join('<br>'));
    function NumSort(a,b) {	return parseInt(a) - parseInt(b) }
    function ShowInfo( srtInfo,flds ) {
      var str = '';
      var tmp = [];
      var temp = [];
      for (var i=0; i<masterArray.length; i++) {
        tmp = masterArray[i].split(',');
        str = '';
        for (var j=0; j<flds.length; j++) { str += ','+tmp[flds[j]]; }
      switch (srtInfo) {
    	case 'aa' : temp.sort(); break;
    	case 'na' : temp.sort(NumSort); break;
    	case 'ad' : temp.sort().reverse(); break;
    	case 'nd' : temp.sort(NumSort).reverse(); break;
    	default: alert('Error has occured: '+srtInfo); break;
    //  document.getElementById('masterInfo').innerHTML = temp.join('<br>');
    // above is test display before table below created
      str = '<table border="1">';
      for (var i=0; i<temp.length; i++) {
    	tmp = temp[i].split(',');
    	str += '<tr><td>'+tmp[0]+'</td><td>'+tmp[1]+'</td></tr>';  
      str += '</table>';
      document.getElementById('masterInfo').innerHTML = str;
    <button onclick="document.getElementById('masterInfo').innerHTML = ''">Clear Display</button>
    <button onclick="document.getElementById('masterInfo').innerHTML = masterArray.join('<br>')">
     Display Master </button>
    <button onclick="var arr=[0,1];ShowInfo('aa',arr)">Fields 0,1 ascending</button>
    <button onclick="var arr=[1,0];ShowInfo('na',arr)">Fields 1,0 ascending</button><br>
    <button onclick="var arr=[0,1];ShowInfo('ad',arr)">Fields 0,1 decending</button>
    <button onclick="var arr=[1,0];ShowInfo('nd',arr)">Fields 1,0 decending</button><br>
    <div id="masterInfo"></div>
    Have fun and good luck!

Thread Information

Users Browsing this Thread

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

Posting Permissions

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

Recent Articles