added the sort logic