I am calling an API with some sports data and i want to orgnise the json into decending order depending on how many 'points' they have. however if two players have the same points, then want to order them them by 'points difference', and if they have the same points difference, then order by 'points for'. for example a random json like so:
let table = [
{team_name: "D", points: 10, points_difference: 45, points_for: 52},
{team_name: "B", points: 12, points_difference: 38, points_for: 60},
{team_name: "C", points: 10, points_difference: 45, points_for: 52},
{team_name: "A", points: 12, points_difference: 40, points_for: 60}
]
Should be organised like this...
let table = [
{team_name: "A", points: 12, points_difference: 40, points_for: 60},
{team_name: "B", points: 12, points_difference: 38, points_for: 60},
{team_name: "C", points: 10, points_difference: 45, points_for: 52},
{team_name: "D", points: 10, points_difference: 45, points_for: 50}
]
i currently organise based on one property, like so:
table.sort((a, b) => b.points - a.points)
but am struggling to implement these other conditions. the data will always be an array of objects. appreciate any help on this!