Sort an array alphabetically, and then reverse the order of the sorted items (descending): - Javascript Array

Javascript examples for Array:sort

Description

Sort an array alphabetically, and then reverse the order of the sorted items (descending):

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
var fruits = ["a","b","c","d","e"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {/*  ww w .  java 2  s.c  om*/
    fruits.sort();
    fruits.reverse();
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>

Related Tutorials