Sorting an array of scores

For example if I have this function name:

function sortScores(myscores){

I would like to take an array of score records (each containing a score and a page number) and return a sorted version where the records are arranged in descending order of score.

so if I have this example call:

sortScores( [ { score:0, Number:0 }, { score:2, Number:1 }, { score:1, NUmber:2 } ] )

I would get the example result:

[ { score:2, Number:1 }, { score:1, Number:2 }, { score:0, Number:0 } ]

Where the results have been sorts in descending order of score.


I’ve looked at this example, but it only deals with one one value –>

http://www.javascriptkit.com/javatutors/arraysort.shtml

function sortScores(myscores){
	return myscores.sort(function(a, b){
		var n= a.score-b.score;
		if(n=== 0){
			return a.Number-b.Number;
		}
		return n;
	});
}

Okay, so how would I call the function, I tried using an alert but it just returned

[object Object],[object Object],[object Object]

I think Number was the wrong name to use so instead it’s this:

call this function:
sortScores( [ { score:0, Num:0 }, { score:2, Num:1 }, { score:1, Num:2 } ] )

I would get the example result:

[ { score:2, Num:1 }, { score:1, Num:2 }, { score:0, Num:0 } ]

with the brackets included :frowning:

If you control the input, use arrays instead of objects.
Arrays will return strings without any work.

function sortScores(myscores){
	return myscores.sort(function(a, b){
		var n= a[0]-b[0];
		if(n=== 0){
			return a[1]-b[1];
		}
		return n;
	});
}

var inp= [[2, 1], [1, 2], [0, 0]];
sortScores(inp).join(’
')

/* returned value: (String)
0,0
1,2
2,1
*/

/*
If are working with objects, you need to make your own strings,
*/

function sortScoresString(myscores){
	var temp, str= '[';
	myscores= myscores.sort(function(a, b){
		var n= a.score-b.score;
		if(n=== 0){
			return a.num-b.num;
		}
		return n;
	});
	for(var i= 0, L= myscores.length; i<L; i++){
		temp= myscores[i];
		str+= '{';
		for(var p in temp){
			if(temp.hasOwnProperty(p)){
				str+= p+':'+temp[p]+',';
			}
		}
		str= str.slice(0, -1)+'},'
	}
	return str.slice(0, -1)+']';
}

var inp= [{score: 2, num: 1},{score: 1, num: 2},{score: 0, num: 0}];
sortScoresString(inp);

/* returned value: (String)
[{score:0,num:0},{score:1,num:2},{score:2,num:1}]
*/

Thanks so much, but if I wanted to sort the scores from ascending to descending order, how would I do that?

Well, in the original code:

myscores= myscores.sort(function(a, b){

This is using the sort() function (which does dictionary sort) with another function to sort numbers.


		var n= a[0]-b[0];          //for sorting the "scores"
		if(n=== 0){                //if scores are equal
			return a[1]-b[1]; //then sort by the "nums"
		}
		return n;

Here the order is set by basically still doing var something = a minus b.

I would assume you could just do b minus a instead for a reverse number sort.

Javascript also has a reverse() function, which does the same as sort() for dictionary sorts, but backwards. But since this is a number sort, I figure just the b minus a thing would do the trick.

What other in built functions can i use instead of “hasOwnProperty”

I have to correct myself above:
you’d keep a-b to reverse the sort, but change the order of the args in the function:
sort(b,a)

propertyIsEnumerable can tell you what’s an actual property of your object, rather than something inherited from some ancestor object.