Code checked, new sorting method to solve sample drawing order issues