Assuming a collection of objects, each of which needs to be drawn at a specific layer, at what point would it (or ever) be better to sort each object by layer rather than looping multiple times and drawing a layer at each pass? More importantly how would you arrive at this conclusion? Bonus points for a sort algorithm you would use if you would sort?
for (obj = each in collection) {
for (i=0; i<=topLayer; i++) {
if (obj.layer == i) {
obj.draw()
}
}
}
/* vs. */
function layerCompare(obj1, obj2) {
return (obj1.layer > obj2.layer)
}
collection.sort(layerCompare)
for (obj = each in collection) {
obj.draw()
}