initial sort logic