From: cpuracr8 on
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.

From: Mark P on
cpuracr8(a)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.
>

If you want the items to move together, why not group them all in a
single class? Then you can define operator< for that class and use sort
on an array of elements of that class? Here are some ideas...

struct StudentData
{
string name;
int idNumber; // I'm making these ints only for convenience
int score;
bool operator< (const StudentData& rhs) const;
};

bool StudendtData::operator< (const StudentData& rhs) const
{
return idNumber < rhs.idNumber;
}

struct Course
{
StudentData roster[7];
};

Course course1;

std::sort (course1.roster, course1.roster + 7);

Really though it seems excessive to have a struct Course just to hold an
array of StudentData so I would work directly with the array:

StudentData course1[7];

Also consider the possibility of using a vector rather than an array if
you're doing anything more complicated than arrays whose sizes are known
at compile time.

-Mark
From: cpuracr8 on
i see what you're saying. i should have added that a stipulation was
that i had to use three parallel arrays to store the data. i thought it
was logical to put them in a struct to keep them together.

From: Karl Heinz Buchegger on
cpuracr8(a)gmail.com wrote:
>
> i see what you're saying. i should have added that a stipulation was
> that i had to use three parallel arrays to store the data.

In this case you can't use a predefined sort function like std::sort
but you need to write your own.

Every sort function has a section where data is swapped. This is the
place where you need take into account that there are 3 parallel arrays.

Another possibility is, that you sort the data in an indirect way.
Create an index array and sort that with respect to the data to sort.
The index then tells you in which order you have to visit the
data entries to get a sorted order:

orignial data

index name id score

0 "ABC" 001221 8
1 "DEF" 001543 12
2 "GHI" 001334 9
3 "JKL" 001189 15

index sorted with respect to id

3
0
2
1

that's because

id[3] equals 001189
id[0] equals 001221
id[2] equals 001334
id[1] equals 001543

and that is a sorted order based on id.
For sorting the index you actually don't need to move
neither name, id nor score around. And since this is so
you don't have to take care that those arrays are in parallel.

You will need the std::sort method which takes an additional argument
for the comparison function to implement that strategy. You sort an
array of int (inititialized with numbers from 0 to n) and in the
comparison function you don't compare the numbers, but compare the
id array entries with the passed numbers used as index into the id array.

--
Karl Heinz Buchegger
kbuchegg(a)gascad.at
From: Heinz Ozwirk on

<cpuracr8(a)gmail.com> schrieb im Newsbeitrag
news:1130484777.099108.78310(a)o13g2000cwo.googlegroups.com...
> 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.

To me, it looks like your structure is wrong. Whenever you have several
arrays where elements at the same position belong together, like your
student[i], idNumber[i] and score[i], a structue with those data is usually
better suited. Then you'dhave all information about one student in one
place:

struct Student
{
string name;
string id;
string score;
};

Then you can declare variable for the courses like

Student course1[7], cource2[7], ...;

or even better

std::vector<Student> course1;
std::vector<Student> course2;
...

And, given a comparision operator for Student structures, like

bool operator<(Student const& lhs, Student const& rhs)
{
return lhs.id < rhs.id;
}

you can sort course1 with

std::sort(course1.begin(), course1.end());

HTH
Heinz


 | 
Pages: 1
Prev: The C++ Answer book?
Next: Basic questions