Last active
October 26, 2018 16:24
-
-
Save corocoto/afea007632bf5f9f3ed29c35d40f4c57 to your computer and use it in GitHub Desktop.
Метод sort в JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numberArray=[60, 50, 62, 58, 54, 54]; | |
numberArray.sort(compareNumbers); | |
console.log(numberArray); | |
/*сортировка в порядке возрастания*/ | |
function compareNumbers(num1, num2) { | |
if (num1>num2) { | |
return 1; | |
} else if (num1===num2){ | |
return 0; | |
}else{ | |
return -1; | |
} | |
} | |
/*сортировка в порядке убывания*/ | |
function compareNumbers1(num1, num2) { | |
if (num2>num1) { | |
return 1; | |
} else if (num1===num2){ | |
return 0; | |
}else{ | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment