cpp implementation of sort_list, more complicated than my estimation earlier