0

This is an array with objects inside of it, how can I sort it so the lowest score is the first one showing in the array?

var theArray = [{username: "A", scores: 12}, 
                {username: "B", scores: 4}, 
                {username: "C", scores: 2}]

So that username: "C" would become the first one in the array.

Thanks a lot in advance.

user2314737
  • 27,088
  • 20
  • 102
  • 114
  • 3
    `theArray.sort((a, b) => a.scores - b.scores)` – Andrew Li Dec 15 '17 at 22:36
  • [Array Sort][1] You'll want to use array.sort. theArray.sort(function(obj1, obj2){ return obj1.scores - obj2.scores;}); This [1]: https://www.w3schools.com/jsref/jsref_sort.asp – N-ate Dec 15 '17 at 22:39

0 Answers0