0

So I create two objects:

var obj1 = {
    "id" : 123,
    "name" : "Hello"
}

var obj2 = {
    "id" : 456, 
    "name" : "Goodbye"
}

Then I insert these into an array:

var arr = [obj1, obj2];

I then store the result of sorting into a variable and log to the console:

var test = arr.sort(function(x, y) {
    console.log("x ", x)
    console.log("y ", y)
});

console.log(test);

The result is:

x  {id: 456, name: 'Goodbye'}
y  {id: 123, name: 'Hello'}

I was expecting the order to be the same as the occurrence of the objects in the array since I have not defined any sort criterion. Why is the order as such?

5
  • 1
    "Why is the order as such?" why does the order matter at all? Commented Dec 14, 2021 at 8:10
  • @VLAZ I was expecting that param "x" would correspond to array element 0 and param "y" would correspond to array element 1. The order does not "matter" I am simply trying to understand why they do not correspond. Commented Dec 14, 2021 at 8:12
  • 2
    Because the sorting algorithm in your environment decided to pick up elements in that order. Commented Dec 14, 2021 at 8:14
  • 3
    the language standard does not support a certain algorithm for sorting, just to return a value from the sorting function which represents the wanted order from two items. more here -> Array#sort Commented Dec 14, 2021 at 8:23
  • 1
    As I have come to understand it, the sorting algorithm used in .sort() is dependent of the data, and it chooses the "best" algorithm, may it be bubble sort, quick sort, selection sort or whatnot. I'm not sure which one .sort() chooses at what time (and it's irrelevant for me to know), but different algorithms will pick items in different orders. Commented Dec 14, 2021 at 8:59

2 Answers 2

1

If you had to just type in arr and press enter after the sort you will see that the array is still in the order of 'hello' and then 'goodbuy' the sort didnt change the data as there is no way for the system to compare the objects unless you create a compare function to give the sort call.

The compare function you have provided just prints the first and second values being compared.

Seen that you cant compare the first value to nothing i.e. it has nothing pervious to compare to it starts at the second entry and goes... I need to compare this to something... Lets try the first element.

if you had to add more elements you would see this clearly.

var arr = [{
  "id" : 123,
  "name" : "Hello"
}, {
  "id" : 456, 
  "name" : "Goodbye"
}, {
  "id" : 999, 
  "name" : "Its clear now"
}];
var test = arr.sort(function(x, y) {
  console.log("x ", x)
  console.log("y ", y)
});

console.log(test);

Output

So as you can see it compares second to first it then compares third to second. The sort is complete.

Doing the same with numbers and actually sorting

enter image description here

you get

position 2 compares to position 1 and returns -1 so its smaller and must moves into position 1

enter image description here

[2,3,1,6,5]

position 3 compares to position 1 and returns -1 so its smaller and must moves into position 1

enter image description here

[1,2,3,6,5]

position 4 compares to position 1 and returns 5 so its larger and must remain in place

enter image description here

[1,2,3,6,5]

position 4 compares to position 2 and returns 4 so its larger and must remain in place

enter image description here

[1,2,3,6,5]

position 4 compares to position 3 and returns 3 so its larger and must remain in place

enter image description here

[1,2,3,6,5]

position 4 has nothing in front of it to compare to so we move to the next position

position 5 compares to position 1( of the original array) and returns 2 so its larger and it uses normal binary search logic to try another position (next compare)

enter image description here

[1,2,3,6,5]

finally position 5 compares to position 4 and returns -1 so it knows it must move into position 4

enter image description here

[1,2,3,5,6]

Sign up to request clarification or add additional context in comments.

3 Comments

"So as you can see it compares second to first it then compares third to second" in the environment you have a screenshot of (I assume Chrome). And also for this input. That's definitely not how sort works everywhere.
@VLAZ very true. I took the screen shots in Edge (chromium based). Was just more of an example as to why the sort was printing out 'unexpected' values as per the question. Good to point out that its not the same everywhere as that would be a bad assumption. Thanks mate.
@JacquesRamsden very helpful. Thanks for taking the time to explain.
0

sort() documentation, from documentation it is implementation-defined in case you don't provide valid sorting function, if you want to keep it same way you can return 0

var obj1 = {
    "id" : 123,
    "name" : "Hello"
}

var obj2 = {
    "id" : 456, 
    "name" : "Goodbye"
}


var arr = [obj1, obj2];

var test =arr.sort((x,y)=>{
   console.log("x ", x)
    console.log("y ", y)
    return 0;
 }
)

/* var test = arr.sort(function(x, y)) {
    console.log("x ", x)
    console.log("y ", y)
    return 0;
}); */

console.log(test);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.