Sorting A Vector Of Structs By Contained Data

I am sorry if there is similar questions already present on the website, but am currently failing to understand certain parts of the algorithm.

I have a Struct that contains information on user account information for my game:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    struct Account
    {
    	int Position;
    	string Name;
    	int Score;
    	string Date;
    	int Level;
    
    	bool operator < (User SOMETHING, User SOMETHING)
    	{
    	     return (SOMETHING < SOMETHING);
    	}
    };

	vector<Account> User;
    User.push_back(Account());
    User.push_back(Account());
    User.push_back(Account());

    User[0].Position=1;
    User[1].Position=2;
    User[2].Position=3;

    sort(User.begin(), User.end(), Account);


I need each struct of my vector to be to be organized, say for instance, in descending/ascending order the position number that each contains.

I just need help on (1) bool operator function (e.g. parameters and return values), and (2) How do I have it so that I can sort it by multiple variables like the positions, scores & level. (Would I need to have 3 bool operator functions?)
Last edited on
First of all, your operator is incorrect: it should be either non-member function taking 2 arguments or member function takin one argument.
Second, it looks like it is intended to compare Account elements, but take User ones.
Third, you are declaring two variables with same name in it.

(2) What do you exactly want? To sort simultaneously (first by name and if names are same sort by position) or three different types of sorting?

(1)
http://en.cppreference.com/w/cpp/language/operators
http://www.cprogramming.com/tutorial/operator_overloading.html
http://www.learncpp.com/cpp-tutorial/91-introduction-to-operator-overloading/
http://www.learncpp.com/cpp-tutorial/94-overloading-the-comparison-operators/

Exaample of overloading operators and using predicates: http://ideone.com/0RXCx0
@MiiNiPaa

For one instance, I need them to be sorted according to the value in "Position" for each struct.

So, if User[0].Position is equal to 3 and User[2] is equal to 1, then switch them. (If we are doing ascending order, and the reverse for descending)

But, I also need to do it for the the scores at a different.

They don't need to be simultaneous, just one at a time.
Well, I have posted a link to example. You need a "predicate" example (both sort call and predicate definition.)
Just define 3 of them comparing different fields.
Topic archived. No new replies allowed.