我想按照“用户”对象内的“名称”对下面的数组进行排序。
如何做到这一点?
我有一种方法可以对对象数组进行排序,但无法用于对象数组中的对象。
这是该方法:
然后我可以使用这个进行排序:
var myArr = [
{"id":1,"user":{"name":"allen","id":101}},
{"id":2,"user":{"name":"martin","id":102}}
]
如何做到这一点?
我有一种方法可以对对象数组进行排序,但无法用于对象数组中的对象。
这是该方法:
function dynamicSort(property) {
var sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a, b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
然后我可以使用这个进行排序:
myArr.sort(dynamicSort("id"));