Sort array of objects

how would I sort this array so the member with the highest points prints first?

[FONT=Courier New]
	var members = [{
		name: "stephen gupta",
		points: 52
	},{
		name: "laura kowalski",
		points: 67
	},{
		name: "John Rodriguez",
		points: 33
	}];
	[/FONT]

how would I sort this array so it prints the name with the most points first?

for testing am printing contents thus:

[FONT=Courier New]for (var i = 0; i < members.length; i++) {
	     $('#theMembers').append('name: '   + members[i].name +
				' -- points: '  + members[i].points  + '<br>' );
	}
  	[/FONT]

but how do I sort it so the member with the most points prints first?
I know array has .sort() method, but how does it work for a more complex array like this one?
(is this an array of objects or a three-dimensional array? :wink: is it the same thing?

thank you…

What you can do with that is to supple the sort method with a function that controls how the sorting occurs.


members.sort(function (a, b) {
    return (a.points > b.points) ? 1 : -1;
});

You’ll also need to use an array index in the for loop to access the array items in the members array.

It’s an array of objects.

A three-dimensional array would be where each array item contains another array too.


var three_d = [
    [
        ['0,0,0', '0,0,1', '0,0,2'],
        ['0,1,0', '0,1,1', '0,1,2'],
        ['0,2,0', '0,2,1', '0,2,2']
    ],
    [
        ['1,0,0', '1,0,1', '1,0,2'],
        ['1,1,0', '1,1,1', '1,1,2'],
        ['1,2,0', '1,2,1', '1,2,2']
    ]
];

thank you…

but I don’t see here an example with an array that has the same structure as my array… these kinds of arrays can have a million different structures… they’re like JSON objects… it drives me nuts…

everyplace where I look this up I see

[FONT=Courier New]members.sort(function (a, b) {.... }[/FONT]

isn’t this a comparison fn?

if I want to sort by points how do I compare just two values? what if I have eight elements in the array?

there’s no simple way to say print the object with the highest no. of points first?

this is for a test, there are 6 questions, this is the only one I can’t do, I have been working on it for hours… I have never worked with this kind of array and I find this really hard…

this is the test:


[FONT=Courier New]var members = [{
		name: "stephen gupta",
		points: 52
	},{
		name: "laura kowalski",
		points: 67
	},{
		name: "John Rodriguez",
		points: 33
	}];
[/FONT]

  1. write a function that sorts the array by “points”
  2. write a function that creates an li elemnt for every item in the array, ex:
[FONT=Courier New]<li>name: stephen gupta<br>points: 52</li>[/FONT]

I have been trying to do this all day, and I can’t… have found lots of stuff on stackoverflow, but nothing is exactly what I want…

thank you…

There is, and that is to sort the contents so that it is ordered with the highests number of points being first, as according to the example that I gave.
Here is some sample code that demonstrates it in action, too.
http://jsfiddle.net/pmw57/gKGbY/

your fiddle works!! :slight_smile:

thank you…

(sorry, I find the code for this confusing…)

so how about if I want to sort by points in reverse order?

thank you…

You would have a separate function to sort them in reverse.


function comparePoints(a, b) {
    return (a.points > b.points) ? 1 : -1;
}
function comparePointsReverse(a, b) {
    return (a.points < b.points) ? 1 : -1;
}

members.sort(comparePoints); // points in increasing order
members.sort(comparePointsReverse); // points in decreasing order