General object ordering

Took object ordering code for entities and made it generic so it can be
used for anything.