how to Sort the array in order in js code example
Example 1: sort js array array
var ages = [18, 21, 9, 41, 35, 24]
ages.sort(function(a, b) {
return a - b
})
// => [9, 18, 21, 24, 35, 41]
Example 2: sort() function example JavaScript
function ascendingOrder(arr) {
return arr.sort(function(a, b) {
return a - b;
});
}
ascendingOrder([1, 5, 2, 3, 4]);
Example 3: JavaScript Sorting Arrays
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits