Sorting parallel vectors
I have some parallel vectors that need to be sorted. With my knowledge I set them up to be sorted such as this:
Well, since I've only covered arrays, I setup as above using swap. As you guys know there is no swap in vectors. Can anybody give me a hint as to what to look for? I know there is a sort function but I need to sort the vectors in parallel.
void sortInventory(vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<double>& manufPrice, vector<double>& sellingPrice)
int i, j;
for (i = 0; i < itemID.size()-1; i++)
min = i;
for (j = i + 1; j < noOfRows; j++)
if (itemName[j] < itemName[min])
min = j;
That looks fine to me, you just need to use the proper swap. (That code wouldn't work for vectors or arrays.)
The syntax for swap is:
Thank you Daved, again my textbook is wrong. It shows to swap you would do array[i].swap(array[j]).
That might work for a vector that holds certain class types that have a swap function. For example, it would work for vector<string> because string has a swap function.
Surely, this should be kept in ONE vector holding a struct like this:
Now you can even use the sort function for your vector [assuming you make a compare member for your class]. Or, if you wan to sort it yourself, you can just swap one block with the other.