implement function to sort stack