# Thread: Sorting another vector based on how one vector is sorted?

1. ## Sorting another vector based on how one vector is sorted?

So I have a vector holding numbers and another vector holding strings.
They both have equal number of elements. The number's elements represent the string's elements. So the number's element and string's element can change indexes but should have the same index eventually.

Now I have to sort the number's vector in descending order. However, I also have to sort the vector holding strings exactly how I sorted the number's vector.

Suppose these are the two vectors:
number's: 1, 54, 32
string's: God, God's Diapers, Diaper's God

So after sorting it should be like this:
number's: 54, 32 ,1
string's: God's Diapers, Dieaper's God, God

Makes sense?

2. Since the data is related have you considered using a structure or class to hold this data?

3. Originally Posted by jimblumberg
Since the data is related have you considered using a structure or class to hold this data?
Can you give me an example?

4. If this is an assignment where you're expected to be implementing the sort yourself, then you should write the code to sort vector A and then, every time you do a swap in vector A, you also do the same swap in vector B.

If this is real life, then these should be in an object of some kind.

5. Originally Posted by Nwb
Can you give me an example?
Code:
```struct Thing {
int id;
std::string name;
};```
and then write a comparator function that checks the IDs to determine the sorting order.

6. By the way it's not an assignment and I'm not exclusive to any particular method.

You see, I don't know how many elements the numbers' and strings' vectors have. I get this info at runtime. I form two vectors depending on what the user inputted, such that I have two equally sizes vectors storing the numbers and storing the strings.

If I could do this with structures or class then I'm clueless.

7. Originally Posted by Nwb
By the way it's not an assignment and I'm not exclusive to any particular method.

You see, I don't know how many elements the numbers' and strings' vectors have. I get this info at runtime. I form two vectors depending on what the user inputted, such that I have two equally sizes vectors storing the numbers and storing the strings.
The fact that you don't know how many items there are is orthogonal to the question of how many related items there are. If there is one ID number, corresponding to one string, then that is what your object is.

The whole point of a vector is to deal with the fact that there are going to be an unknown number of these things. So you define these Things (presumably you would give them a more descriptive name based on what they are), make a std::vector of these Things, push all your items on it, then do a std::sort. (To make the sort work, you'll either need to write a comparator function -- the default is operator<, but since you want things sorted in descending order apparently, I would use a different name.)

8. Umgh yes the number is like the ID of the string.

"So you define these Things" - sorry what do I define? I don't know how to define a structure at run time can you give me an example? And I'm pretty much blanking after that line..

Okay imagine that I'm a 5 year old. How would you speak to a 5 year old? Yes that's me.

I interpreted what you said was that you think I should define the structures beforehand and then assign values to them? But then what if I had to support 200 some values.. so that wouldn't work.. and probably that's not what you meant either..

9. Originally Posted by Nwb
Umgh yes the number is like the ID of the string.

"So you define these Things" - sorry what do I define? I don't know how to define a structure at run time can you give me an example? And I'm pretty much blanking after that line..
You can't, but that's okay, because you don't want to. The object is the pair of <id, string> and that's defined right now, not at run time. How were you planning to handle the unknown number of things before? In a vector (well, a pair of vectors). The vectors deal with the quantity of things; the object deals with the relationship between the id and the string.

Code:
```struct Thing {
int id;
std::string name;
};

bool compare_the_things(const Thing &a, const Thing &b) {
return a.id > b.id;
}

int main(void) {
std::vector<Thing> pile_of_things;
//however your info is stored you'll have to build the vector
while(still things to read) {
Thing temp;
read in data to temp;
pile_of_things.push_back(temp);
}
//now sort the things
std::sort(pile_of_things.begin(), pile_of_things.end(), compare_the_things);
//then you can walk through the list
}```

10. By the way if you're always going to compare the structure one way you could substitute an overload of the operator< in the class instead of the compare() function.

11. Ohhh that makes sense. That helps a lot thanks!!

@jim, I don't get it.. I'm a bit dumb.. so ugh.. I didn't get what you're saying. And yes I will need to compare the structures a lot. I don't get how what when where < is getting overloaded or how to overload it or why to overload it.

12. Then I suggest you use your favourite search engine and look into C++ operating overloading.

Edit: By the way what book are you using to learn C++?

13. Originally Posted by jimblumberg
Then I suggest you use your favourite search engine and look into C++ operating overloading.

Edit: By the way what book are you using to learn C++?
This might sound strange but I don't have any C++ books or anything I just learnt little bits of C++ from videos online and references here and there.. Do you know if there are any online sources that I can refer? I might buy a book but I'm just really really lazy.

So jim you're saying I should overload the operater >, but why would I need to do that? And why would it be inside the class? If I did overload >, why would I do that and would I lose the native function of >?

14. This might sound strange but I don't have any C++ books or anything I just learnt little bits of C++ from videos online and references here and there.
No this doesn't surprise me since you're jumping all over the place and don't seem to have any real guidance.

Do you know if there are any online sources that I can refer?
Not really. I don't believe that most people can really learn a computer language by relying only on on line sources.

I might buy a book but I'm just really really lazy.
I suggest you get over this and find and use a book. Any book, even a horribly bad book, will at least have some structure and teach you the basics before you get into more advanced topics. Also talking about lazy, you really need to learn to research your questions before posting to a forum. You can probably find the answers much faster if you do this research before relying on some forum, no matter how many forums you use.

So jim you're saying I should overload the operater >
No, I never said that.

First said operator< not operator>, there is a big difference. Most of the standard algorithms use std::less (the operator<) by default. If you want some other comparison operator you must either use a comparison function (similar to what has already been shown) or some how inform the compiler to use something other than std::less.

Secondly I also said "you could", not "you should", again big difference.

And why would it be inside the class?
I said inside the class because most of the time you want to be able to access any member of the class, not just the public members and operator overloads inside the class are usually easier because they sometimes take fewer parameters. But you can overload some operators outside the class, you just need to insure that you are using the correct parameters.

If I did overload >, why would I do that and would I lose the native function of >?
There are no "native" operators for User Defined Types (classes/structures) to lose. If you want an operator you must write the code, again you need to do some research on operator overloading in C++ there is a lot of information available than what I'm willing to write in a forum post.

15. Can I get online book copies (umgh I live in the north pole so I don't know if they ship there..)? Do I have to pay (we don't get good wages in the north pole, you see)?

Okay I clearly have no clue what you're talking about but I'll read about it thanks jim.

Popular pages Recent additions