updated code

added the sorted function to the previous exercise