C++ sorting structure

skavt

Honorable
Dec 4, 2013
4
0
10,510
So, i need help sorting a structure in a c++ simple console app, i need to sort it by the variable "cas"(time), and i can't figure out how to do it because the objects dont have the same name...would apreciate some help, here is the code:

struct tekmovalec
{
int sstevilka;
char ime[10];
char priimek[15];
double cas;
char nacionalnost[3];
char ime_ekipe[15];
}t1,t2,t3,t4,t5,t6;

void uredipocasu()//sortbytime/bubblesort
{
int pom;
for(int i = 0; i < 6; i++)//pass 6 times
{
for(int j = 1; j < 5; j++)
{
if(t[j].cas < t[j+1].cas)
{
pom = t[j].sstevilka;
t[j].sstevilka = t[j+1].sstevilka;
t[j+1].sstevilka = pom;
pom = t[j].ime;
t[j].ime = t[j+1].ime;
t[j+1].ime = pom;
pom = t[j].priimek;
t[j].priimek = t[j+1].priimek;
t[j+1].priimek = pom;
pom = t[j].cas;
t[j].cas = t[j+1].cas;
t[j+1].cas = pom;
pom = t[j].nacionalnost;
t[j].nacionalnost = t[j+1].nacionalnost;
t[j+1].nacionalnost = pom;
pom = t[j].ime_ekipe;
t[j].ime_ekipe = t[j+1].ime_ekipe;
t[j+1].ime_ekipe = pom;
}
}
}
}
 
Solution
Why not create an array of objects (t[0], ..., t[5]) rather than 6 individual objects (t1, ..., t6)? This is, essentially, how you are treating them in your sort routine.

skavt

Honorable
Dec 4, 2013
4
0
10,510
Sorry for not answering earlier, but i got pulled into the program and when i completed it, i totally forgot about you guys.

Anyway, I ended up doing an array of objects as Ijack suggested, but i didn't know that was possible till i saw your post, and from there on everything went pretty easy. Finished the program in like 20 minutes :) Thanks