Home  >  Article  >  Web Front-end  >  Share your in-depth understanding of sort

Share your in-depth understanding of sort

零下一度
零下一度Original
2017-06-30 10:28:051027browse

Callback function: 

//回调函数:把一个方法A当做参数值传递给另外一个函数B,在B执行的过程中,我们随时根据需求让A方法执行function fn(callback){
            callback();
        }
        fn(function(){})

sortIn-depth understanding of the code

var ary = [12,23,14,34,23,1,14,16,26,2];
        ary.sort(function(a,b){//a->每一次执行匿名函数的时候,找到的数组中的当前项//b->当前项的后一项return a - b;//升序 如果a>b,返回大于0的数,a和b交换位置return b - a;//降序 如果b>a,返回的>0,a和b交换位置//我return的是一个>0 / <=0的数,大于0 让a和b交换位置,小于等于0原来的位置不动        })

        ary.sort(function(a,b){return 1;//不管a和b谁大,每一次都返回一个恒大于0的数,也就是每一次a和b都要交换位置,最后的结果就是原有数组倒过来排列了==>reverse})
 ary ='小郭',age:78'小将',age:13'小王',age:107'小陈',age:256 parseFloat(a.age) -

The above is the detailed content of Share your in-depth understanding of sort. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn