Let’s say you have a JavaScript class like this
var DepartmentFactory = function(data) {
this.id = data.Id;
this.name = data.DepartmentName;
this.active = data.Active;
}
Let’s say you then create a number of instances of that class and store them in an array
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}));
So I now would have an array of objects created by DepartmentFactory
. How would I go about using the array.sort()
method to sort this array of objects by the DepartmentName
property of each object?
The array.sort()
method works just fine when sorting an array of strings
var myarray=["Bob", "Bully", "Amy"];
myarray.sort(); //Array now becomes ["Amy", "Bob", "Bully"]
But how do I make it work with a list of objects?