Completed custom sorting logic