-1

We have a Mario Kart tournament at work and I wanted to try and log the results using basic JavaScript.

Here's the code: https://jsbin.com/hafati/44/edit?js,console

I've nearly got there, but I need help sorting the results.

Here's the console.log of the data:

[object Object] {
  1: [object Object] {
    id: 1,
    name: "Player 1",
    total: 20
  },
  2: [object Object] {
    id: 2,
    name: "Player 2",
    total: 60
  },
  3: [object Object] {
    id: 3,
    name: "Player 3",
    total: 80
  },
  4: [object Object] {
    id: 4,
    name: "Player 4",
    total: 400
  },
  5: [object Object] {
    id: 5,
    name: "Player 5",
    total: 150
  },
  6: [object Object] {
    id: 6,
    name: "Player 6",
    total: 50
  }
}

How would you sort these players, with the highest total first?

3
  • 2
    I would put them in an array not an object, then sort them Commented Jul 22, 2015 at 12:39
  • indeed, make results an array instead of Object, then the sort will work Commented Jul 22, 2015 at 12:40
  • 1
    I'll give that a go, thanks guys! @Mike The title of that post was misleading, but seems to be a similar problem. Thanks for the heads up! Commented Jul 22, 2015 at 12:43

3 Answers 3

3

JavaScript array functions are polymorphic, so you can use them on "array like" objects just fine:

var obj = { 0 :  {total: 3}, 1 : {total: 1}, 2 : {total: 5}}; // your object
obj.length = 2; // need to set this manually.

var sorted = [].sort.call(obj, function(x,y){ return x.total - y.total; });

That said, it is always best to store sequential data in a sequential data structure like an array - so while this works it's not the best idea. I'm assuming you don't have a choice :)

Sign up to request clarification or add additional context in comments.

1 Comment

You can also keep them sorted in a sorted set.
1

Your need to store data in array of objects, this will be easier to iterate over array than objects and will also ensure the order of elements. Then you can use array sort function to sort the array elements.

Demo

var myArr = [{
  id: 1,
  name: "Player 1",
  total: 20
}, {
  id: 2,
  name: "Player 2",
  total: 60
}, {
  id: 3,
  name: "Player 3",
  total: 80
}, {
  id: 4,
  name: "Player 4",
  total: 400
}, {
  id: 5,
  name: "Player 5",
  total: 150
}, {
  id: 6,
  name: "Player 6",
  total: 50
}];


myArr.sort(function(a, b) {
  return a.total < b.total;
});
console.log(myArr);

2 Comments

@daveaspinall if this answer solved your issue please consider accepting an answer.
I have to wait five minutes before I can accept, already tried :-) Will of course accept when it lets me.
0

Put them in a array:

var score = [{
    id: 1,
    name: "Player 1",
    total: 20
  },{
    id: 2,
    name: "Player 2",
    total: 60
  },
  ...
}];

Then use Array.sort():

score.sort(function (a, b) {
   return a.total < b.total;
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.