Skip to content

Instantly share code, notes, and snippets.

@corocoto
Last active October 26, 2018 16:24
Show Gist options
  • Save corocoto/afea007632bf5f9f3ed29c35d40f4c57 to your computer and use it in GitHub Desktop.
Save corocoto/afea007632bf5f9f3ed29c35d40f4c57 to your computer and use it in GitHub Desktop.
Метод sort в JS
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