一个数据中的元素都是对象,如何通过不同的K值进行排序呢
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)