Sort函数小Tips!

Sort() 方法用于对数组的元素进行排序,在原数组上操作不生成副本

1、字母排序

const zm = ['abc','cba','dda','hjkh'];
zm.sort();

2、数字排序

const sz = ['12','453','748465','23132'];

sz.sort((a,b) => {
    return b-a; (a-b)  升序  / 降序
})

3、混合数组排数

const arry = [
    {name:"a",num:"20"},
    {name:"b",num:"210"},
    {name:"c",num:"220"},
    {name:"d",num:"230"},
]

function sortarry(property) {
     return function(a,b){
             var  v1 = a[property];
             var  v2 = b[property];
             return v1-v2;/v2-v1;   升序/降序
    }
}
arry.sort(sortarry('num'));