• js排序方法


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>数组操作</title>
    </head>
    <body>
        <script type="text/javascript">
        /*var _arr = [1,2,3,4,5,6,7,8,9];
        var _arr1 = [1,2,3];
        _arr.forEach(function(item,index,_arr){
            document.write(item+"---" +index+ "----" +_arr );
            document.write("<br/>");
        });
        _arr.filter(function(item,index,_arr){
            document.write(item+"----"+index+"----"+_arr+"----"+ this.length);
            document.write("<br/>");
        },_arr1);*/
        //根据数组的和排序
    /*    var _arr3 = [[2,5,66,2],[2,7,11,9],[9,22,3]];
        _arr3.sort(function(a,b){        
            var num1=0;
            var num2=0;
            var i=0;
            for(i=0; i<a.length;i++){
                num1+=a[i];
            }
            var j=0
            for(j=0; j<b.length; j++){
                num2+=b[j];
            }
            return num2-num1;
        });
        document.write(_arr3 + "<br/>");*/
        //根据数组中的最大值排序
        /*var _arr3 = [[2,5,66,2],[2,7,11,9],[9,22,3],[9,101,3]];
        _arr3.sort(function(a,b){
            var num1=0;
            var num2=0;
            a.forEach(function(item){
                if(item>num1){
                    num1=item;
                }
            });
            b.forEach(function(item){
                if(item>num2){
                    num2=item;
                }
            });
            return num2 - num1;
        });
        document.write(_arr3 + "<br/>");*/

        //根据正负从大到小或者从小到大排序
        /*var _arr = [3,2,4,6,1,9,5];
        function sorts(symbol,arr){
            if(symbol == "positive"){
                arr.sort(function(a,b){
                    return a-b;
                });
            }else if(symbol == "minus"){
                arr.sort(function(a,b){
                    return b-a;
                });
            }
            
        }
        sorts("minus",_arr);
        document.write(_arr);*/

    /*    Array.prototype.sHift=function(){
            var _this = this;
            var num = _this[0];
            var i=0;
            for(i=0; i<_this.length; i++){
                _this[i]=_this[i+1];
            }
            return num;
        }
        var _arr = [3,2,4,6,1,9,5];
        _arr.sHift();
        document.write(_arr);*/

        //伪装一个splice函数
        var _arr2 = [3,2,4,6,1,9,5];
        Array.prototype.mySplice=function(){
            var _this = this;
            if(arguments.length==2){
                if(arguments[1] == 0){
                    return _this;
                }else{
                    var i=0;
                    for(i=arguments[0]+arguments[1]; i>arguments[0]; i--){
                        var j=0;
                        for(j=i; j<_this.length; j++){
                            _this[j-1]=_this[j];
                        }                
                    }
                    _this.length = _this.length - arguments[1];
                    return _this;
                }            
            }
            if(arguments.length>2){
                var i=0;
                for(i=arguments[0]+arguments[1]; i>arguments[0]; i--){
                    var j=0;
                    for(j=i; j<_this.length; j++){
                        _this[j-1]=_this[j];
                    }                
                }
                _this.length = _this.length - arguments[1];

                var n=0;
                for(n=2;n<arguments.length;n++){
                    var m=0;
                    for(m=_this.length;m>arguments[1]-1;m--){
                        _this[m] = _this[m-1];
                    }
                    _this[m] = arguments[n];
                }
                return _this;
            }
        }
        //var a = _arr2.mySplice(1,2,"wo");
        </script>
    </body>
    </html>

  • 相关阅读:
    weblogic无需用户名密码启动Server
    中文和unicode互转
    spring集成activeMQ
    jvm分析(MD语法)
    解决java.lang.NoClassDefFoundError: org/apache/log4j/Level
    httpclient 支持代理和http & https
    容器配置jndi Tomcat为例
    java https tomcat 单双认证(含证书生成和代码实现) 原创转载请备注,谢谢O(∩_∩)O
    java Http原生 Get 和Post 支持代理认证
    解决客户端访问https报错
  • 原文地址:https://www.cnblogs.com/qibingshen/p/5734715.html
Copyright © 2020-2023  润新知