Sorted array (join (), reverse (), sort ())

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>数组排序方法(join()、reverse()、sort())</title>
    <!-- 
        n.join (): convert all values ​​of n array is a string type () can be set within a different delimiter, the default display is a comma separated value;
        n.reverse (): all values ​​in the order n of the array is reversed;
        n.sort (): all values, the sort direction of the array of n can be changed to create a function:
                    The default is to sort the values ​​are translated into strings and then, according to the first digit from small to large
                    n.sort (function (a, b) {return (ab)}) represents the ascending
                    n.sort (function (a, b) {return (ba)}) represents the descending
        Mixed use:
        . N.join () reverse (): all values ​​of n in the array into a string, while the order is reversed;
     -->
</head>
<body>
    <script>
        //n.join()
        var num1=new Array(1,2,3,4);
        document.write (num1.join () + " <br /> " ); // 1,2,3,4 / default comma 
        document.write (num1.join ( " - " ) + " <br / > " ); // 1-2-3-4 / used herein - spaced 
        // n.reverse () 
        var num2 = new new the Array ( " One " , " II " , " three " );
        document.write (num2.reverse () + " <br /> " ); // three, II, One / reverse order 
        // n.reverse () the Join (). 
        var num3 = [ " store " , " Wu " , " Lu " ]
        document.write (num3.reverse () the Join (. " ~ " ) + " <br /> " ); // LU Ng ~ store ~ / ~ reversed while using symbolic links 
        // n.sort () 
        var num3 = [ 2 , 8 , 65 , - . 9 ]
        document.write (num3.sort () + " <br /> " ); // -9,2,65,8; and changed the string, the first 8 digits maximum 
        document.write (num3.sort ( function ( A, B) { return A - B;}) + " <br /> " ); // -9,2,8,65; ascending 
        document.write (num3.sort ( function (A, B) { return B - A}) + " <br /> " ); // 65,8,2, -9; DESC 
    </ Script > 
</ body > 
</ HTML >

 

Guess you like

Origin www.cnblogs.com/vinson-blog/p/12006298.html