sort vectors based on other vectors?

how do i sort a vector based on the sorting of another vector? this is probably an iterator question, but i have no idea how to do this.

say for instance i have three vectors: <city> <country> <population>

so the input looks something like

paris france 7812411
cairo egypt 3312310


and so on. it is easy to sort the populations in ascending order using the sort command, but how do i translate this to the string vectors to preserve the "row" order from the initial input?
Last edited on
When you swap v0[x] with v0[y], also swap v1[x] with v1[y] and v2[x] with v2[y].
It would be much easier if you had an vector of a struct or class containing the city, country and population.

You could then use the STL search algorithm with a custom compare function (compare the populations in the struct).
for further information see: http://www.cplusplus.com/reference/algorithm/sort.html

Using this solution you'll also be able to add further fields without any further effort!
Topic archived. No new replies allowed.