implements lexicographic version of sort