假设你有一个这样的 JavaScript 类
var DepartmentFactory = function(data) {
this.id = data.Id;
this.name = data.DepartmentName;
this.active = data.Active;
}
假设您然后创建了该类的多个实例并将它们存储在一个数组中
var objArray = [];
objArray.push(DepartmentFactory({Id: 1, DepartmentName: 'Marketing', Active: true}));
objArray.push(DepartmentFactory({Id: 2, DepartmentName: 'Sales', Active: true}));
objArray.push(DepartmentFactory({Id: 3, DepartmentName: 'Development', Active: true}));
objArray.push(DepartmentFactory({Id: 4, DepartmentName: 'Accounting', Active: true}));
所以我现在会有一个由DepartmentFactory
. 我将如何使用该array.sort()
方法按DepartmentName
每个对象的属性对这个对象数组进行排序?
该array.sort()
方法在对字符串数组进行排序时工作得很好
var myarray=["Bob", "Bully", "Amy"];
myarray.sort(); //Array now becomes ["Amy", "Bob", "Bully"]
但是我如何使它与对象列表一起工作?