Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js sort 1 or -1

function compare(a, b) {
  if (a is less than b by some ordering criterion) {
    return -1;
  }
  if (a is greater than b by the ordering criterion) {
    return 1;
  }
  // a must be equal to b
  return 0;
}
Comment

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 ]
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript confirm tab close 
Javascript :: set cursor type javascript 
Javascript :: Unexpected token a in JSON at position 
Javascript :: angular generate module with rooting 
Javascript :: jest expect error to be thrown 
Javascript :: js select element inside of div 
Javascript :: web-vitals react 
Javascript :: play audio javascript 
Javascript :: disable formcontrol angular 
Javascript :: check if localstorage key exists js 
Javascript :: how to remove special characters from a string in javascript using regex 
Javascript :: console.time in javascript 
Javascript :: excel datatable 
Javascript :: js poll dom 
Javascript :: how get checkbox if checked in jquery 
Javascript :: encodeuri hashtag 
Javascript :: debug react vscode 
Javascript :: call function on modal open 
Javascript :: livewire upload file progress indicator 
Javascript :: get query params from url javascript 
Javascript :: react-native-reanimated npm 
Javascript :: javascript check if time is less than 
Javascript :: how to make a rectangle in javascript 
Javascript :: react multiple event handlers] 
Javascript :: javascript count occurrences in string 
Javascript :: postcss.config.js 
Javascript :: nextjs localstorage 
Javascript :: react 17 hot reload not working 
Javascript :: eas build apk 
Javascript :: vue v-for object 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =