I have a custom sort function for sorting an array of arrays, but I'm receiving different results whether I use if...else statement or ternary operator.
const arr = [[9,0],[7,0],[1,9],[3,0],[2,7],[5,3],[6,0],[3,4],[6,2],[5,2]]
arr.sort((a,b) => {
if(a[0] === [b[0]]) {
return a[1] - b[1]
} else {
return b[0] - a[0]
}
})
gives [[3,0],[6,0],[7,0],[5,2],[3,4],[6,2],[5,3],[2,7],[9,0],[1,9]] but
arr.sort((a,b) => {
return a[0] === b[0] ? a[1] - b[1] : b[0] - a[0]
})
gives [[3,0],[6,0],[7,0],[5,2],[3,4],[5,3],[6,2],[2,7],[9,0],[1,9]]
Why is this happening?
Aucun commentaire:
Enregistrer un commentaire