# How to sort array without changing order of same values

You can check this by your self:

``````function ranking(people) {
return people.sort((a, b) => b.points - a.points);
}

console.log(
ranking([
{
name: "John",
points: 100
},
{
name: "Bob",
points: 130
},
{
name: "Mary",
points: 120
},
{
name: "Kate",
points: 120
}
])
);
``````

This is the output I got from the same function:

``````[0:	{name: "Bob", points: 130}
1:	{name: "Mary", points: 120}
2:	{name: "Kate", points: 120}
3:	{name: "John", points: 100}
]
``````

Which sorts according to points. Now you need to figure out how to add position, to each by looping through each entry

Sort is only part of the solution. You need to figure out how to add positions. Try to do it, then ask for help when you get stuck.

But the object with name `kate` should be before the `mary ` object.

See the coding’s instructions:

Your sort function only takes care of the points. You need to add logic for

if two or more persons have same number of points, they should have same position number and sorted by name (name is unique)

Also, you need to add the position to your objects in the array.

1 Like

It would be beneficial for you to revisit arrays, especially adding to arrays If a condition exists. You have a condition, already, you just need to figure out how to loop through each array, add position based on the condition.

I hope you come right. Happy coding.

I did that. You can see that I’m sorting by points

So I need to sort in this condition by name . Not the order?
What is the difference between sort by name and order?

You need to sort by points and then name. You are currently only sorting by points.

Where is your logic that specifies how to order two objects with the same number of points?

1 Like

How to sort them by name?
Sort them all or only with objects that are the same points?

Mod Edit: SOLUTION REDACTED

Could you explain this line of code ?

Thank you

Thank @moseme.t.t

Now I will add `positions` to every object.

I got stuck on another topic. I’m working on `25 + 5 Clock`. You can help me if you want:

The ampty array means useEffect will run once on mount, so maybe set a condition that you want useEffect to run on by parsing a value [value] into the array. So useEffect will only run when that value changes. But I think you will have to open a new discussion and have this one closed. Maybe invite me into the new one.

So how can I run the timer only when I click the button?

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

1 Like

I would write a single comprehensive callback function for your `sort`.

According to the documentation, if the callback function returns a positive number, then b comes before a. If the callback function returns a negative number, then a comes before b. If the function returns a 0, then the two are sorted to the same order.

Currently, the difference in points gets you mostly sorted, but when the difference between the two points is zero, you should compare the names and either return a positive or negative value.

1 Like

Hello everyone,
I’m solving coding challenge Ranking position on codewars.
I wrote the code but it seems that I did not pass the challenge.

My code so far:

``````function ranking(people) {
return people
.sort((a, b) => a.name.localeCompare(b.name))
.sort((a, b) => b.points - a.points)
.map((item, i) => {
item.position = i + 1;
let curr = item.points;

if (people[i + 1] !== undefined) {
let next = people[i + 1].points;

if (curr === next) {
people[i + 1].position = item.position;
}
}

return item;
});
}

let arr = [
{
name: "John",
points: 100
},
{
name: "Bob",
points: 130
},
{
name: "Mary",
points: 120
},
{
name: "Kate",
points: 120
}
];

console.log(ranking(arr));
``````

the 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 my output is little different:

``````[
{
name: "Bob",
points: 130,
position: 1,
},
{
name: "Kate",
points: 120,
position: 2,
},
{
name: "Mary",
points: 120,
position: 3,
},
{
name: "John",
points: 100,
position: 4,
},
]
``````

The different is that the object with `Kate` name should equal with the object `Mary` name. But my code doesn’t do that!!!