sort array ascending order javascript code example
Example 1: javascript sort array of ints
function sortNumbers(a, b) {
return a - b;
}
var ages = [12, 95, 43, 15, 10, 32, 65, 78, 8];
ages.sort(sortNumbers);
Example 2: sort by ascending javascript
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
Example 3: javascript sort array in ascending order
function myFunction() {
points.sort(function(a, b){return a-b});
document.getElementById("demo").innerHTML = points;
}
Example 4: how to sort array least to greatest javascript stACK
function sortArray(array) {
var temp = 0;
for (var i = 0; i < array.length; i++) {
for (var j = i; j < array.length; j++) {
if (array[j] < array[i]) {
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
return array;
}
console.log(sortArray([3,1,2]));