...

View Full Version : Sorting function not working properly



SGTBlitz
11-14-2010, 01:18 PM
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


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);

Philip M
11-14-2010, 02:53 PM
There is no purpose in trying to re-invent the wheel, but making it oval this time. :D Use this example:-


<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."

Logic Ali
11-14-2010, 03:25 PM
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.

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 );

}

Philip M
11-14-2010, 06:39 PM
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.

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! :D )

Logic Ali
11-14-2010, 07:47 PM
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.

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);

}

Philip M
11-14-2010, 08:23 PM
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. :o



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum