Transpose your data array, add a row on top, fill it with A, B, A, B......, sort ascendingly, delete all A, and transpose data back. Piece of cake!