C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sort a struct of arrays

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
cpuracr8@gmail.com
Guest





PostPosted: Fri Oct 28, 2005 7:59 am    Post subject: sort a struct of arrays Reply with 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.

Back to top
Stefan Näwe
Guest





PostPosted: Fri Oct 28, 2005 9:29 am    Post subject: Re: sort a struct of arrays Reply with quote



[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





PostPosted: Fri Oct 28, 2005 10:01 am    Post subject: Re: sort a struct of arrays Reply with quote



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





PostPosted: Fri Oct 28, 2005 10:06 am    Post subject: Re: sort a struct of arrays Reply with quote

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;
}

Quote:
};




Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.