发布于 2021-04-12 18:33:33
我能够在我的数组上使用.sort()方法设置默认排序。
这是一个对属性进行排序的函数。如果您想要升序和降序,可以将返回顺序从a更改为b。
function dynamicSort(property) {
return function (a, b) {
return b[property] - a[property];
};
}
然后使用sort()方法调用带有要排序的属性名称的函数。
trucks.sort(dynamicSort("status"));
https://stackoverflow.com/questions/67063223
复制