0

Hi I have a game that I am creating with javascript. I has a leaderboard and every 10th person the sockets in the game make an extra room. So from person 11 a different room will be created and the leaderboard from room 1 doesn't show in room 2.

Now the problem: When the 11th person comes in the sort goes haywire. the code:

users.sort(function (a, b) {
    return b.score - a.score;
});

eg: with 8 people showing names with alphabet when everyones score is 0

a, b, c, d, e, f, g, h

eg: with 11 people showing names with alphabet when everyones score is 0

b, f, c, d, e, a, g, h, i, j, k

and the sort moves around the 0,1 and 5 indexes every 2 seconds.

It ONLY happens when the 11th person joins. I have tried decreasing person per room to 5 and still the sort goes weird after the 11th person. If there is anyone that might be able to solve this problem please let me know.

Just for more info, when a person connects to a room, they are pushed to a users[] array and should always be at the bottom default with 0 score. If there is any other info you need please let me know.

EDIT: The name is associated with the score. I have written it so that the user is connecting in that alphabetical order. When user A connects the the sort should end with user A only. Then when B connects because he is pushed to the users array, the sort should be A then B in that order, and when C connects the same thing occurs respectively.

EDIT: User structure.

var currentPlayer = {
    id: socket.id,
    level: 1,
    x: position.x.round(2),
    y: position.y.round(2),
    angle: 0,
    score: 0,
    type: type,
    radius: radius,
    facing: 0,
    room: nameOfRoom,
    lastChatSent: 0,
    name: ""
};

Thanks

6
  • 5
    the only order constraint you have here is on the score but you are expecting them to be returned in alphabetical order? Also, what is the data structure for the people? Commented Nov 3, 2015 at 15:26
  • If all scores are 0, then the output you are showing is correct, since you are only sorting based on score. Commented Nov 3, 2015 at 15:30
  • Is this your desired result: Users with identical score remain sorted in the order they were added to the list? Commented Nov 3, 2015 at 15:31
  • To Culme, yes, that is what I want, but when the 11th person joins, the sort goes weird as said above. Commented Nov 3, 2015 at 15:32
  • Each person has a name: the above mentioned alphabetical letter. And a score which is associated with this letter. Each of the scores is 0, but the order still changes every time the sort is called. Commented Nov 3, 2015 at 15:33

1 Answer 1

1

assuming you have a name property on a person. If scores match then sort by name, otherwise sort by score.

users.sort(function (a, b) {
    var equal = b.score == a.score;
    if(equal){
        return a.Name < b.Name ? -1 : a.Name > b.Name ? 1 : 0;
    }
    else{
        return a.score < b.score ? -1 : a.score > b.score ? 1 : 0
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that Dhunt, that worked perfectly, I changed the Name section to ID though because we know the IDs will be different for sure just in case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.