Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2007
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Sorting function not working properly

    Hi guys,

    I am having a problem where the sorting function I have designed is supposed to sort an array of records into descending order in terms of each record's "score" value, I would expect the below code to alert 2,1,0 in that order but it's as if the code didn't even sort it.

    Help would greatly be appreciated

    Code:
    var g=[{score:0,index:0},{score:2,index:1},{score:1,index:2}]
    
    function sortScores(scoreRecs){
    	var swapped;
    	do
    		for(i=0;i<scoreRecs.length-1;i++){
    			if(scoreRecs[i].score<scoreRecs[i+1].score){
    				var a=scoreRecs[i]
    				var b=scoreRecs[i+1]
    				var c=a;
    				a=b;
    				b=c;
    
    				swapped=true;	
    			
    			}
    			else{
    				swapped=false;
    			}
    		}
    	while(swapped==true)
    }
    
    sortScores(g);
    	alert(g[0].score);
    	alert(g[1].score);
    	alert(g[2].score);

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    There is no purpose in trying to re-invent the wheel, but making it oval this time. Use this example:-

    Code:
    <script type = "text/javascript">
    
    function sortBy(prop){return function(a,b){return a[prop]>b[prop] ? 1 : a[prop]<b[prop] ? -1 : 0; }}
    
    var myArray = [{"name":"Roses","qty":9},{"name":"Violets","qty":18}, {"name":"Buttercups","qty":"3"}, {"name":"Daisies","qty":"9"}];
    var len = myArray.length;
    document.write("Original Data <br>");
    for (var i = 0; i<=len-1; i++) {
    document.write (myArray[i].name + " " + myArray[i].qty + "<br>");
    }
    document.write ("<br>");
    
    document.write("Sorted By Name <br>");
    myArray.sort(sortBy("name"));
    for (var i = 0; i<=len-1; i++) {
    document.write (myArray[i].name + " " + myArray[i].qty + "<br>");
    }
    document.write ("<br>");
    
    document.write("Sorted By Quantity <br>")
    myArray.sort(sortBy("qty"));
    for (var i = 0; i<=len-1; i++) {
    document.write (myArray[i].name + " " + myArray[i].qty + "<br>");
    }
    document.write ("<br>");
    
    
    </script>
    To sort in descending order, change return function(a,b){ to return function(b,a){

    "Good judgment comes from experience and experience comes from bad judgment."
    Last edited by Philip M; 11-14-2010 at 02:08 PM.

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by SGTBlitz View Post
    Code:
    				var a=scoreRecs[i]
    				var b=scoreRecs[i+1]
    				var c=a;
    				a=b;
    				b=c;
    Swapping the values of temporary variables has no effect on the content of the array. Also always enclose a do-while loop body inside braces; you get away with it here because the body amounts to a single clause.
    Code:
    function sortScores(scoreRecs)
    {
       var swapped, a;
       
       do{      
       
          for( var i=0; i < scoreRecs.length-1; i++)
          {
             if( ( swapped = scoreRecs[i].score < scoreRecs[i+1].score ) )
             {
                a = scoreRecs[i];
                scoreRecs[i] = scoreRecs[i+1];
                scoreRecs[i+1] = a;
             }
         }
       
       }while( swapped );  
       
    }

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by Logic Ali View Post
    Swapping the values of temporary variables has no effect on the content of the array. Also always enclose a do-while loop body inside braces; you get away with it here because the body amounts to a single clause.
    Code:
    function sortScores(scoreRecs)
    {
       var swapped, a;
       
       do{      
       
          for( var i=0; i < scoreRecs.length-1; i++)
          {
             if( ( swapped = scoreRecs[i].score < scoreRecs[i+1].score ) )
             {
                a = scoreRecs[i];
                scoreRecs[i] = scoreRecs[i+1];
                scoreRecs[i+1] = a;
             }
         }
       
       }while( swapped );  
       
    }
    Slight snag - that aims to sort alphabetically, not numerically so

    var g=[{score:2,index:0},{score:12,index:1},{score:9,index:2}, {score:21,index:4}, {score:32,index:5}]

    yields 12, 21 ,32, 9, 2. (Still something wrong - wheel now octagonal! )
    Last edited by Philip M; 11-14-2010 at 06:04 PM.

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Philip M View Post
    Slight snag - that aims to sort alphabetically, not numerically so
    We are comparing integers here using < so I'm not sure what you mean.
    I'm sure you spotted the real problem.
    Code:
    function sortScores(scoreRecs)
    {
       var swapped, a;
       
       do{      
          swapped = false;
          
          for( var i=0; i < scoreRecs.length-1; i++)
          {
             if( scoreRecs[i].score < scoreRecs[i+1].score ) 
             { 
                swapped = true;
                a = scoreRecs[i];
                scoreRecs[i] = scoreRecs[i+1];
                scoreRecs[i+1] = a;
             }
         }
       
       }while(swapped);  
       
    }

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    That's OK now!
    And no, I had not spotted the problem. All I noticed was the incorrect sort when more values added, and as mine works did not bother to investigate.

    It is always a good idea to test code thoroughly before posting, though. I always test my code - mostly to check that it does not contain typos, misplaced brackets etc. which it usually does to start off with.


  •  

    Posting Permissions

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