implement z-sort but some bugs are contained.