@loop sty current_pos ; position in the index lda (sorted_indexz),y ; y coordinate of object ldx sorted_indexz,y ; object number/index stx current_index @in_right_place cmp (sorted_indexz-2),y ; compare to y coordinate of next object in index bcs @next_position ldx sorted_indexz-2,y ; move compared objects index to the right sorted index list stx sorted_indexz,y dey dey @compare cmp (sorted_indexz-2),y bcs @found_place ldx sorted_indexz-2,y stx sorted_indexz,y dey dey bne @compare @found_place ldx current_index stx sorted_indexz,y @next_position ldy current_pos iny iny cpy #2*NUM_SPR_OBJ bcc @loop