javascript array sorting – not working as expected

I have a simple array I need to sort, but for some reason the sort function is not working.

In a nutshell I was to sort the array in DESC order based on “weeks_won”. I am using the code below:

players.sort(function(a, b){ return b.weeks_won - a.weeks_won});

The input array is (taken from a console.log):

0 {player_name: "Chuck-Norris", weeks_won: 2, correct_picks: 21, total_picks: 32, pick_ratio: 66}
1 {player_name: "Grasslb", weeks_won: 0, correct_picks: 7, total_picks: 11, pick_ratio: 64}
2 {player_name: "JackSparrow", weeks_won: 0, correct_picks: 15, total_picks: 32, pick_ratio: 47}
3 {player_name: "tech-guy-schneider", weeks_won: 0, correct_picks: 12, total_picks: 27, pick_ratio: 44}

Answer

It works fine. Please try run snippet.

const players = [
  {player_name: "Chuck-Norris", weeks_won: 2, correct_picks: 21, total_picks: 32, pick_ratio: 66},
  {player_name: "Grasslb", weeks_won: 0, correct_picks: 7, total_picks: 11, pick_ratio: 64},
  {player_name: "JackSparrow", weeks_won: 0, correct_picks: 15, total_picks: 32, pick_ratio: 47},
  {player_name: "tech-guy-schneider", weeks_won: 0, correct_picks: 12, total_picks: 27, pick_ratio: 44}];

players.sort(function(a, b){ return b.weeks_won - a.weeks_won});

console.log(players);