美文网首页
索引数组(正/倒/反转)

索引数组(正/倒/反转)

作者: 智多牛 | 来源:发表于2016-12-20 10:06 被阅读0次

/**
* 数组
*/
var num = [1,3,2,4,6,5]

    /**
     * 正序
     */
    function pros(a,b)
    {
        return a > b
    }
    
    /**
     * 倒序
     */
    function cons(a,b)
    {
        return a < b
    }
    
    /**
     * 反转
     */
    function reverse(a,b)
    {
        return a = b
    }
    
    console.log('反转:'+num.sort(reverse)) //输出:5,6,4,2,3,1
    
    console.log('正序:'+num.sort(pros))    //输出:1,2,3,4,5,6
    
    console.log('倒序:'+num.sort(cons))    //输出:6,5,4,3,2,1

相关文章

网友评论

      本文标题:索引数组(正/倒/反转)

      本文链接:https://www.haomeiwen.com/subject/xuuxvttx.html