title: Fast sorting where order seldom changes
body: <p>I'm working on a 2D game with a scrolling view (think Red Alert or Zelda) but I'm stuggling with the drawing.</p>
  
  <p>Basically there are two types of objects that are drawn on the map. Some have fixed positions (like trees and buildings) and others move around (players, enemies, flying arrows).</p>
  
  <p>For things to appear in front of each other in a correct way they need to be drawn in a specific order (distant objects first and work towards the "camera").</p>
  
  <p>Right now I'm sorting the list of all objects (both kinds) every time the game updates (100 times per second) and it feels a like a huge waste of CPU time. The order of the objects very seldom changes, and when they do they usually only move one position up or down in the list.</p>
  
  <p>Another issue is that only the objects that are actually on screen needs to be be considered. As the maps could become quite large with 1000s of objects I don't want to sort them all 100 times per second.</p>
  
  <p>How would you suggest I solve this?</p>