gets the sort logic just right