对于这个想法,"key"变量变为一个键数组:然后指定要排序的嵌套值的"路径".
function keysrt(arr, keyArr, reverse) { var sortOrder = 1; if(reverse)sortOrder = -1; return arr.sort(function(a, b) { var x=a,y=b; for (var i=0; i < keyArr.length; i++) { x = x[keyArr[i]]; y = y[keyArr[i]]; } return sortOrder * ((x < y) ? -1 : ((x > y) ? 1 : 0)); }); } keysrt(b,['data','title']);