Sorting of a string vector based on an integer vector

Hi , I have two vectors vector<string>names={"Raymond","Cynthia","David","William","Mike"} , then i am doing a get_mark() call on each student to get vector<int>marks={50,80,45,25,90} . Now i need to sort both vectors based on marks of student , ie result has to be vector<int>marks_sorted={25,45,50,80,90} and vector<string>names_sorted={"William","David","Raymond","Cynthia","Mike"} . One method i can think is do sort of marks first then compare sorted and unsorted marks vector and then sort names , but is there an elegant way of doing this ? Thanks
One way to do it is to use a bubblesort or some other algorithm on the vector marks. When you need to swap two marks you also swap the according names in the names vector.
Have you considered std::vector<std::pair<std::string,int>> that holds the name-mark pairs?

Then provide a comparator for this pair-type that ensures proper ordering in sort.
Topic archived. No new replies allowed.