how would I sort this array so the member with the highest points prints first?
<font face='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 face='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? 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 = [
[
[']
]
];
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 face='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 face='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 face='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...
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.
your fiddle works!!
thank you..
(sorry, I find the code for this confusing...)
so how about if I want to sort by points in reverse order?
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