一個數據中的元素都是對象,如何經過不一樣的K值進行排序呢spa
var o1 = {name:'jhon',age:22}; var o2 = {name:'frank',age:30}; var o3 = {name:'hehe',age:15}; var ol = [o1,o2,o3]; function compareByKey(prop){ return function(item1,item2){ var value1 = item1[prop]; var value2 = item2[prop]; if(value1<value2){ return -1; } else if(value1==value2){ return 0; } else{ return 1; } } } var fun = compareByKey("age"); ol.sort(fun)