I am testing method sort()
to array.
I don't understand how it works and I want to understand how it works step by step. For example:
let array=[8, 9, 1, 2, 3, 4];
console.log(array);
array.sort(function (a, b) {
console.log(`a = ${a} , b = ${b}`);
return a - b;
});
console.log(array);`
I got such results:
[ 8, 9, 1, 2, 3, 4 ]
a = 9 , b = 8; a = 1 , b = 9; a = 1 , b = 9; a = 1 , b = 8; a = 2 , b = 8; a = 2 , b = 1; a = 3 , b = 8; a = 3 , b = 2; a = 4 , b = 3; a = 4 , b = 9; a = 4 , b = 8
[ 1, 2, 3, 4, 8, 9 ]
Can you explain how is new array built step by step? How can I watch the result step by step?