By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 440,640 Members | 1,598 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

# sort a struct of arrays

 P: n/a 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. Oct 28 '05 #1
Share this Question
3 Replies

 P: n/a cp******@gmail.com wrote: 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 Oct 28 '05 #2

 P: n/a 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; } }; Oct 28 '05 #3

 P: n/a Rolf Magnus wrote: 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 Oct 28 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion.