假如我们想实现,把这样一个数组排下序,先按一个属性排,再按另一个属性排
var arr = [{cezuProjectName: '1',group: 'a'}, {cezuProjectName: '2',group: 'b'}, {cezuProjectName: '1',group: 'b'}, {cezuProjectName: '1',group: 'a'}];
可以使用如下方法
function compare(obj1, obj2){ var val1 = obj1.cezuProjectName; var val2 = obj2.cezuProjectName; if(val1 < val2){ return -1; } else if(val1 > val2) { return 1; } else { var val3 = obj1.group; var val4 = obj2.group; if(val3 < val4){ return -1; } else if(val3 > val4){ return 1; } else { return 0; } } }
ffff
标签:obj1,group,对象,else,var,数组,cezuProjectName,return,排序 From: https://www.cnblogs.com/shiyujian/p/9518167.html