JavaScript array in ascending and descending order

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数组升降序排列</title>
</head>
<body>
    <script type="text/javascript">
        var arr = [10,1,60,100,30,25,3];
        function aa(i,j){
             return i - j;   // ij is in ascending order, 
        }
         function bb(i,j){
             return j - i; // ji is in descending order 
        }
         // So when calling the sort method, you need to put The aa function is passed in, remember not to say that aa is passed in 
        alert(arr.sort(aa));
        alert(arr.sort(bb));
    </script>
    
</body>
</html>

Effect picture:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325249141&siteId=291194637