<!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: