-
Sorting structures
Next problem :-)
I have created a class to do several things. It defines a structure that contains 4 columns. I have a list of these structures contained in an array. What i am wanting to do is allow the user to define with column they would like to sort by but I cant quite figure out what I need to do. I figured I could write a functions for each column, but am unclear on how to implement it. I can provide my entire code, or snippets if necessary; else just point me in the right direction please!
-
Why not use a std::vector instead of an array?
You can just make four functions. Each for every column. Then the value you use to sort will be that column, but when you swap, you can swap the structure itself. For example, you have two structures lets say in a sorting function:
Code:
void sortCol1(myStruct[] structs)
{
...
if (myStruct[0]->column1 > myStruct[1]->column1)
{
//swap structures
myStruct* temp = myStruct[0];
myStruct[1] = myStruct[0];
myStruct[0] = temp;
}
}
You can combine then the 4 functions into one and add the column as a parameters.
-
Well, Im using arrays because the assignment requires. :-(
That explanation does help though. Only thing im confused about is how to call the columns.
My Declaration of class and structure looks like this
Code:
class Format{
char dlimit;
int count;
ifstream infile;
ofstream outfile;
struct {
string c1,c2,c3,c4;
} title;
struct {
string c1,c2,c3,c4;
} aline;
struct {
string name;
float float1;
float float2;
int int1;
} list[100];
-
> string c1,c2,c3,c4;
Well saying
string columns[4];
would make it a lot easier to say (using a number) which column you want.
-