Possible Duplicate:
Sort JavaScript array of Objects based on one of the object’s properties
I have an object which has a property called z:
function building(z) { this.z = z; } Let's say I create 3 instances of this object:
a = new building(5) b = new building(2) c = new building(8) These instances are then placed into an array
buildings = [] buildings.push(a) buildings.push(b) buildings.push(c) The Question
How would I sort this array IN ASCENDING ORDER based on the z property of the objects it contains? The end result after sorting should be:
before -> buildings = [a, b, c] sort - > buildings.sort(fu) after -> buildings = [b, a, c]