The most viewed posts

Registry Reviver

How to clean up Windows Computer

About Desktop Icons in Windows Computer

Driver Booster

CCleaner

Myanmar font and keyboard for Windows

Advanced System Care

some (JavaScript Array Method)

How to make a computer secure and run faster

How to sell IT materials in zilastar.com

The most viewed items

Desktop Hard Disk

Dell Inspiron 5559

HP ProBook 430

SONY Playstation Portable

Laptop RAM

External Hard Disk

Laptop

ZilaStar ICT Jul 24 2021, 19:46:34
Category : JavaScript Tutorials

sort (JavaScript Array Method)

//1. sort alphabetically
/**
const countries = ["Myanmar", "Singapore", "Malaysia", "USA", "Thailand", "Vietname"];

function tnwFun1(){
document.getElementById("demo").innerHTML = countries.join("
");
}

function jsSort(){
let result = countries.sort();
document.getElementById("demo").innerHTML = result.join("
");
}

function jsReverse(){
let result = countries.reverse();
document.getElementById("demo").innerHTML = result.join("
");
}

//2. Sort randomly

const tnwNums = [20, 17, 30, 38, 40, 50, 28, 12];

function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNums.join("
");
}

function tnwFun2(){
let result = tnwNums.sort(tnwFun3);
document.getElementById("demo").innerHTML = result.join("
");
}

function tnwFun3(a, b){
return 0.5 - Math.random();
} */

// 3. Sort Numerically

const tnwNums = [20, 17, 30, 38, 40, 50, 28, 12, 4];

function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNums.join("
");
}

function jsSort(){
let result = tnwNums.sort(function(a, b){return a - b;});
document.getElementById("demo").innerHTML = result.join("
");
}

function jsReverse(){
let result = tnwNums.sort(function(a, b){return b - a;});
document.getElementById("demo").innerHTML = result.join("
");
}

  • Comments:

    No comment to show.

    To Contact
    Available Services
    Terms and Conditions