Sort dependencies and remove duplicates in dependencies