use lexicograph sorting alg