implement the sorting