In this example, we are passing calculation as (b-a). function sortArray() { arr.sort(function(a, b){return b-a}); document.getElementById("sample").innerHTML = arr; } See the code snippet:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="sortArray()">Sort the array</button>
<p id="sample"></p>
<script>
var arr= [36, 96, 3, 5, 35, 10];
document.getElementById("sample").innerHTML = arr;
function sortArray() {
arr.sort(function(a, b){return b-a});
document.getElementById("sample").innerHTML = arr;
}
</script>
</body>
</html>