Sorting array of structs

struct dadosEquipas{

char nomeEquipa[30];

int lugarCampeonato;

int numPontos;

int numJogos;

int numVitorias;

int numEmpates;

int numDerrotas;

int golosMarcados;

int golosSofridos;

int diferencaGolos;

} infequipas[18];

I need to sort this array by the decreasing order of numPontos. Once it's sorted, if there are multiple positions with an equal number of numPontos, it sorts only them by the decreasing order of diferencaGolos.
std::sort accepts an extra parameter that lets you customize the sort order:
http://www.cplusplus.com/reference/algorithm/sort/
http://en.cppreference.com/w/cpp/algorithm/sort
Ok thx
I've got this code now:

struct dadosEquipas{

char nomeEquipa[30];

int lugarCampeonato;

int numPontos;

int numJogos;

int numVitorias;

int numEmpates;

int numDerrotas;

int golosMarcados;

int golosSofridos;

int diferencaGolos;

} infequipas[18];

struct myclass {
bool operator() (dadosEquipas abc, dadosEquipas ABC) { return (abc.numPontos<ABC.numPontos); }
} myobject;

std::vector<dadosEquipas> myvector(infequipas, infequipas + 18);

std::sort(myvector.begin(), myvector.begin() + 18/2);

std::sort(myvector.begin() + 18/ 2, myvector.end(), myobject);

std::sort(myvector.begin(), myvector.end(), myobject);

It's returning the error C2893: Failed to specialize function template 'unknown-type std::less<void>::operator ()(_Ty1&&,_Ty2 &&) const', but in the external dependecy algorythm.

The first time you call std::sort you forget to give it myobject.

By the way, change this line:

bool operator() (dadosEquipas abc, dadosEquipas ABC)

To this instead:

bool operator() (dadosEquipas const &abc, dadosEquipas const &ABC)
The programme is not sorting. This is what i changed:

bool operator() (dadosEquipas abc, dadosEquipas ABC) to bool operator() (dadosEquipas const &abc, dadosEquipas const &ABC)

std::sort(myvector.begin(), myvector.begin() + 18/2); to std::sort(myvector.begin(), myvector.begin() + 18/2, myobject);

Did I do something wrong?
miguel96 wrote:
The programme is not sorting.
What do you mean? Does it crash? Does it not compile? Does it not change the order of elements? Does it send your grandmother an email virus? You need to be specific.
Sorry. It doesn't change the order of the elements
Could you repost your current entire code? (use [code] tags too)

I do wonder why you are calling std::sort multiple times - the last time you call it sorts the entire vector, so why even do the partial sorts in the first place?
The entire thing has about 800 lines. This is just the sorting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
struct dadosEquipas{

	char nomeEquipa[30];

	int lugarCampeonato;

	int numPontos;

	int numJogos;

	int numVitorias;

	int numEmpates;

	int numDerrotas;

	int golosMarcados;

	int golosSofridos;

	int diferencaGolos;

} infequipas[18];

struct myclass {
bool operator() (dadosEquipas const &abc, dadosEquipas const &ABC) { return (abc.numPontos<ABC.numPontos); }
} myobject;

std::vector<dadosEquipas> myvector(infequipas, infequipas + 18); 

std::sort(myvector.begin(), myvector.begin() + 18/2, myobject);

std::sort(myvector.begin() + 18/ 2, myvector.end(), myobject);

std::sort(myvector.begin(), myvector.end(), myobject);

Why do you have lines 31 and 33? Line 35 does all you need.

How are you verifying that the data is not sorted? Note that you copy the data into the vector from infequipas and that infequipas is never changed. If you want to change infequipas then sort it and forget the vector:

std::sort(infequipas, infequipas + 18, myobject); //replaces lines 29-35
I use this cycle to see if it is sorted:

1
2
3
4
5
6
for (int i = 0; i < numEquipas; i++){

				cout << setw(5) << infequipas[i].lugarCampeonato << setw(20) << infequipas[i].nomeEquipa << setw(8) << infequipas[i].numJogos << setw(6) << infequipas[i].numPontos << setw(6) << infequipas[i].numVitorias << setw(6) << infequipas[i].numEmpates << setw(6) << infequipas[i].numDerrotas << setw(6) << infequipas[i].golosMarcados << setw(6) << infequipas[i].golosSofridos << setw(6) << infequipas[i].diferencaGolos << endl << endl;
				cout << setfill('=') << setw(80) << "=" << setfill(' ') << endl << endl;

			}


It's sorting now thanks. I had those two extra lines because I took them from the first tutorial that you gave me. I've just started to learn so I still do stupid mistakes like that one.

Once again thanks.
Last edited on
Ah yes, the issue was that you had copied the data into the vector and sorted the vector, but this didn't affect infequipas at all and that was what you were expecting to get sorted. Glad you've got it working now.
Topic archived. No new replies allowed.