 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
cpuracr8@gmail.com Guest
|
Posted: Fri Oct 28, 2005 7:59 am Post subject: sort a struct of arrays |
|
|
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.
|
|
| Back to top |
|
 |
Stefan Näwe Guest
|
Posted: Fri Oct 28, 2005 9:29 am Post subject: Re: sort a struct of arrays |
|
|
[email]cpuracr8 (AT) gmail (DOT) com[/email] wrote:
| Quote: | 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
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
|
|
| Back to top |
|
 |
Stefan Näwe Guest
|
Posted: Fri Oct 28, 2005 10:01 am Post subject: Re: sort a struct of arrays |
|
|
Rolf Magnus wrote:
| Quote: | Stefan Näwe wrote:
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;
}
};
|
Ooops...
Sorry.
Thanks.
Stefan
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Oct 28, 2005 10:06 am Post subject: Re: sort a struct of arrays |
|
|
Stefan Näwe wrote:
| Quote: | 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;
}
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|