fixed sorting algorithm and added a function to sort in descending order