I have array of objects and I want to sort them by points from bigger to smaller. But if two or more persons have same number of points, they should have same position number and sorted by name (name is unique).
For example if you have function:
[
{
name: "John",
points: 100,
},
{
name: "Bob",
points: 130,
},
{
name: "Mary",
points: 120,
},
{
name: "Kate",
points: 120,
},
]
Output should be:
[
{
name: "Bob",
points: 130,
position: 1,
},
{
name: "Kate",
points: 120,
position: 2,
},
{
name: "Mary",
points: 120,
position: 2,
},
{
name: "John",
points: 100,
position: 4,
},
]
But when I try solve this problem, the output was:
My code is:
function ranking(people) {
return people.sort((a, b) => a.points < b.points);
}
What I need to include on my code to solve the problem?