Answers for "js sort return new array"

5

sort array of objects javascript

list.sort((a, b) => (a.color > b.color) ? 1 : -1)
Posted by: Guest on July-23-2020
0

why sort() return - 1 js

var arr=[1,2,3,4,5,6,100,999]
arr.sort((a,b)=>{
  if (a%2==b%2) return a-b;
  if (a%2>b%2) return -1;
  return 1;
})
console.log(arr)

//output: [ 1, 3, 5, 999, 2, 4, 6, 100 ]
Posted by: Guest on April-16-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language