| yonifra (8) | |
|
Hi all, I'm very new to C++ but not so new to programming (I know C language and HTML) Well, what I wanted to ask is how do I implement a sorting mechanism using the famous Merge Sort algorithm to sort various parameters of a class (Say, I have a class named Car with parameters: Color, Type, Model and I want to sort first by type, then model, then by color) I would appreciate an insightful answer :) Thanks! Jonathan. | |
|
|
|
| ropez (310) | |||||||||
|
All you need to do is implement a function to compare two objects of the class. For this, you would normally use either the less than operator, '<', or a "functor". Operator:
Functor:
Using these, you can sort a collection of Car objects using sort functions in the standard library:
If you want to manually implement the sort algorithm, you can compare two objects like this:
There are other standard library functions that you might be interested in: http://www.cplusplus.com/reference/algorithm/merge.html http://www.cplusplus.com/reference/algorithm/swap.html And many others. Good luck. | |||||||||
|
|
|||||||||
| yonifra (8) | |
| Thanks a lot! You really helped a lot! | |
|
|
|