why sort 28 29 return 1 js

Solutions on MaxInterview for why sort 28 29 return 1 js by the best coders in the world

showing results for - "why sort 28 29 return 1 js"
Sofia
19 Jul 2020
1var arr=[1,2,3,4,5,6,100,999]
2arr.sort((a,b)=>{
3  if (a%2==b%2) return a-b;
4  if (a%2>b%2) return -1;
5  return 1;
6})
7console.log(arr)
8
9//output: [ 1, 3, 5, 999, 2, 4, 6, 100 ]
10