Sort function based on problem.cpp