sort a struct of arrays

C

cpuracr8

i've been looking through the topics here and i can't quite find one
that helps me. i'm trying to sort a struct that contains three arrays
using the sort() function. the three arrays are parallel and need to be
grouped together when it is sorted. for instance, if i sort the int
array by ascending order, i need the name and grade associated with
that int array to also move with it. i hope what i said is not too
confusing. what am i doing wrong? here is the code i'm using:

struct courses {
string student[7];
string idNumber[7];
string score[7];

} course1, course2, temp;

course1.student[0] = "Name";
course1.student[1] = "Name";
....
course1.student[6] = "Name";

course1.idNumber[0] = "001221";
course1.idNumber[1] = "001543";
....
course1.idNumber[6] = "001334";

course1.score[0] = "38";
course1.score[1] = "98";
....
course1.score[6] = "67";

sort(course1.idNumber, course1.idNumber+7)
// end

i realize that sorting like what i have above only sorts that one
array. but since i want all the arrays to be linked to each other, i
need to move the other two arrays to the same position when it sorts
the idNumber. is there a different way to do this? the only stipulation
is that i need to have three parallel arrays for each course1 and
course2.
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

i've been looking through the topics here and i can't quite find one
that helps me. i'm trying to sort a struct that contains three arrays
using the sort() function. the three arrays are parallel and need to be
grouped together when it is sorted. for instance, if i sort the int
array by ascending order, i need the name and grade associated with
that int array to also move with it. i hope what i said is not too
confusing. what am i doing wrong? here is the code i'm using:

struct courses {
string student[7];
string idNumber[7];
string score[7];

} course1, course2, temp;

course1.student[0] = "Name";
course1.student[1] = "Name";
...
course1.student[6] = "Name";

course1.idNumber[0] = "001221";
course1.idNumber[1] = "001543";
...
course1.idNumber[6] = "001334";

course1.score[0] = "38";
course1.score[1] = "98";
...
course1.score[6] = "67";

sort(course1.idNumber, course1.idNumber+7)
// end

i realize that sorting like what i have above only sorts that one
array. but since i want all the arrays to be linked to each other, i
need to move the other two arrays to the same position when it sorts
the idNumber. is there a different way to do this? the only stipulation
is that i need to have three parallel arrays for each course1 and
course2.


Why don't you create an array of 'course' objects:

struct course {
string student;
string idNumber;
string score;
};

struct SortCourseById {
bool operator<(const course& a, const course &b) const { return a.idNumber < b.idNumber; }
};

course courses[7];

sort(courses, course+7, SortCourseById);


Or am I missing something ?


Stefan
 
G

Guest

Rolf said:
Stefan Näwe wrote:




ITYM:

bool operator()(const course& a, const course &b) const
{
return a.idNumber < b.idNumber;
}

Ooops...
Sorry.
Thanks.

Stefan
 
R

Rolf Magnus

Stefan said:
struct course {
string student;
string idNumber;
string score;
};

struct SortCourseById {
bool operator<(const course& a, const course &b) const { return
a.idNumber < b.idNumber; }

ITYM:

bool operator()(const course& a, const course &b) const
{
return a.idNumber < b.idNumber;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top