JS对象数组排序

我这个是根据子集长度来排序的,按需求更改

				let videos = res.data;
 				var compare = funcvtion (obj1, obj2) {
                    var val1 = obj1.videoUps.length;
                    var val2 = obj2.videoUps.length;
                    return val2 - val1;
                }
                videos.sort(compare);
                videos.splice(4,videos.length);
                
				根据对象某个属性大小来排序
				var arr = [
				    {name:'zopp',age:0},
				    {name:'gpp',age:18},
				    {name:'yjj',age:8}
				];
				
				function compare(property){
				    return function(a,b){
				        var value1 = a[property];
				        var value2 = b[property];
				        return value1 - value2;
				    }
				}
				arr.sort(compare('age'))

你可能感兴趣的:(JavaScript)