How to sort array without changing order of same values

Correct, but we can make a slight streamline.

This is equivalent of what was said elsewhere:
(x.position != y.position) ? x.position - y.position :

In non-Ternary form:
if (x.position != y.position) { x.position - y.position } else {

Note that sort in Javascript doesnt need a -1 or 1 specifically, it needs a value < 0, > 0 or = 0.
Given that X and Y are two numbers that are not the same:
If X is greater than Y, X-Y will be positive. It doesnt matter what value it is, it will be positive.
If Y is greater than X, X-Y will be negative. Again, it doesnt matter what value it is, it will be negative.

So we can capture both cases in a single mathematical expression. Note again that this only works because X and Y are numbers; can’t do this with strings. At least, not directly.

1 Like

Thank you @Paul_Wilkins for this GREAT explanation. I learned too much from this coding challenge from googling, try solve it by myself and asking coding communities.

I have two questions.

  1. Could you explain this line of code:
  1. Why did you put position in?:

position is a number value not a prop?!!

1 Like

[quote=“abdulrahmanmhdanas, post:44, topic:372889, full:true”]
I have two questions.

  1. Could you explain this line of code:

Yes, that’s a ternary operator.

It does the same thing as the following much more expanded code:

if (a.name < b.name) {
    return -1;
} else {
    return 1;
}

The variable called position contains a number. I could have assigned it as follows:

return Object.assign(item, {
    "position": position
});

Or as follows without the double quotes:

return Object.assign(item, {
    position: position
});

But, JavaScript has a property shorthand for that. When the property has the same name as the variable, you can use just the variable to represent both the property and the value.

return Object.assign(item, {
    position
});

Or, brought up on to one line as I used in the code:

return Object.assign(item, {position});

I don’t make a habit of squeezing things on to just one line. I aim to go for understandability. The ternary and property shorthands do help to achieve both goals and can be quite handy, when used in the right place.

I know that you are using ternary operator. But I don’t understand what this code is doing?

The code is in a sort function. The purpose of the sort function is to instruct sort about how to order what is being sorted.

Assume that a.name is Mark and b.name is Mary. In that case a is less than b and so -1 is returned, telling sort that a goes before b.

Alternatively assume that a.name is Mary and b.name is Mark. In that case a is not less than b and so 1 is returned, telling sort that b goes before a.

This description of how sort works gives further details that might be helpful too.

I read the documentation. It was helpful.

It’s also why I told Allan to be careful using Lexicographical (“Dictionary Order”) sorting.

“azzzzzzzzz” < “ba” is true, because the thing looks at the first letter, sees “a” comes before “b” in the dictionary, and sorts it that way. It doesnt need to look at any of the other letters - once it’s found a difference, it can make a decision and stop.

Lets take some points + name combinations, as was suggested.
“100Macy” and “100Mark”, would sort correctly, because it would compare “letter” by letter -
“1” = “1”, keep going.
“0” = “0”, keep going.
“0” = “0”, keep going.
“M” = “M”, keep going.
“a” = “a”, keep going.
“c” != “r”, stop; I can determine that “100Macy” goes first based on this difference, i don’t need to check any further. So this sorts correctly.

“120Macy” and “140Amy”, would not sort correctly, because it would compare “letter” by letter -
“1” = “1”, keep going;
“2” != “4”, stop. I can determine that “120Macy” goes first based on this difference (Note that this is actually backwards; you want the lower number to go SECOND, not first.)

But even if the order wasnt backwards, there are deeper problems.

The big problem comes when you start using this comparisson on string-numbers of different magnitudes.

“1000Jim” vs “30Mark” - based on character-by-character sorting, the system will think that 1000Jim comes first - even though Jim’s point total was 1000, and Mark’s was 30, the comparison doesn’t zero-pad the smaller number, so things dont go right.

This is why you often see things like:
1
10
11
12
13
2

8
9

  • the system is doing a Lexicographical sort, and is treating the numbers as strings.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.