0

I was trying to understand the steps of a sorting function so I know when a[1] - b[1] and b[0] - a[0] are performed, so I inserted a console.log in my compare function. I am more confused as I don't see a pattern, for example why is [7, 1] [4, 4] logged twice?

const sortFunc = (a, b) => {
    console.log(a,b)

    return a[0] === b[0] ? a[1] - b[1] : b[0] - a[0];
}

let arr = [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

arr.sort(sortFunc) // sorted arr is [[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]

the console logs are as follow:

[4, 4] [7, 0]
[7, 1] [4, 4]
[7, 1] [4, 4]
[7, 1] [7, 0]
[5, 0] [7, 1]
[5, 0] [4, 4]
[6, 1] [5, 0]
[6, 1] [7, 1]
[5, 2] [6, 1]
[5, 2] [4, 4]
[5, 2] [5, 0]
2

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.