[HELP] Sorting Structure ??

Hi, i have a struct that has some data of a few persons like in the sample i have here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

        struct PersonalData
        {
               int
                char *FirstName;
                char *LastName;
                char *Birthday;  // in the format of 12/30/1978
                int  id;
        };


        PersonalData PersonOne;

        // Populate PersonOne with data
        PersonOne.FirstName = "John";
        PersonOne.LastName = "Doe";
        PersonOne.Birthday = "12/30/1978";
        PersonOne.id = 5;

        // Populate PersonTwo with data
        PersonTwo.FirstName = "Matt";
        PersonTwo.LastName = "Thompson";
        PersonTwo.Birthday = "15/20/1958";
        PersonTwo.id = 3;


Lets say i add even more persons, How can I sort them lets say according to the 'id' so i can print them out in lets say, descending order.
so with higher 'id' value first, and then with lower and so on??


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Sort Function
 void sort(infoType tele[], const int& size)
 {
	
	int i, j;
	int smallest;
	
	for (i = 0; i < size - 1; i++)
	{
		smallest = i;

		for (j = i + 1; j < size; j++)
		{
			if (tele[j].lname != "" && tele[smallest].lname != "")
			{	
				 if( tele[j].lname <  tele[smallest].lname )
				{
					smallest = j;
				}
			}
		}
			//swap
			infoType temp = tele[smallest];
			tele[smallest] = tele[i]; 
			tele[i] = temp;              
	}
}


I used the above in a previous program to sort by last name. Maybe you can just modify this?
Also, once you have them sorted, it doesn't matter the order either ascending or descending because you can correct that with a loop. It would be easier just to sort them accordingly with the sort function the first time but even if you do not you can just use a loop to give what you want. First I would try to sort them how I wanted. Below is what I was talking about with the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>

using namespace std;

int main()
{
	int one[5] = {1,2,3,4,5};
	
	// This goes one way
	for(int i = 0; i < 5; i ++)
	{
	  cout << one[i] <<" ";
	}
	
	cout << endl;
	
	//// This goes the other 
	for(int i = 4; 0 <= i; i--)
	{
		cout << one[i] <<" ";
	}

	system("pause");
	
	return 0;
}
ye, i know how to sort arrays, you go through the array indexes with a for loop, but how do you cicle through a structure? -i dont know how to use it in a structure...

array:
for(int i=0; i<5; i++){
cout << array[i];
}

but how for a structure??

for(int i=0; i<5; i++){

WHAT HERE?? :S

}

i dont know how i can call a certain element of a structure to cicle through... :/
1
2
3
4
5
6
7
8
9
for(int index = 0; index < size; index++)
	{
		cout << tele[index].lname << endl;
		cout << tele[index].fname << endl;
		cout << tele[index].streetAdd << endl;
		cout << tele[index].cityStateZ << endl;
		cout << tele[index].phone << endl;
		cout << endl;
	}


You can print it like above. If you read my example above with the sort function you will see that it says if( tele[j].lname < tele[smallest].lname )
That's using a structure. .lname
Is that what you want?
Last edited on
Thank you, but sorry for one more noob question
whats the 'tele' part for? o.O

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   struct PersonalData
        {
               int
                char *FirstName;
                char *LastName;
                char *Birthday;  // in the format of 12/30/1978
                int  id;
        };


        PersonalData PersonOne;

        // Populate PersonOne with data
        PersonOne.FirstName = "John";
        PersonOne.LastName = "Doe";
        PersonOne.Birthday = "12/30/1978";
        PersonOne.id = 5;

        // Populate PersonTwo with data
        PersonTwo.FirstName = "Matt";
        PersonTwo.LastName = "Thompson";
        PersonTwo.Birthday = "15/20/1958";
        PersonTwo.id = 3;



from my code i use

tele[index].FirstName

but whats the 'tele' in my code?? o.O
noob I don't know what this means but don't apologize for any question.
When I did the assignment I took that from I used an array. You said that you might want to add more people to this eventually? A vector would be better but I do not know much about those.
This is how I organized my own project.

This was my structure.
1
2
3
4
5
6
7
8
9
//Structure
struct infoType
{	
	string fname;
	string lname;
	string streetAdd;
	string cityStateZ;
	string phone;
};

Then I used an array.
declared as such
infoType tele[NSIZE];

You already have the sort function above.
That's as far as I got lol :)
I'm just suggesting maybe you can you can do something similar. I say that vectors are better because I know you can add or subtract from the size.
Just advice take it or leave it.
I'm new to programming also I saw your post and said oh I have done something similar before.
OK, thanks for the reply, but i cant get it to work :S:S:S:S:S:S:S

SO can anyone make me an example implementation using the code i posted??? with my variables...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  struct PersonalData
        {
               int
                char *FirstName;
                char *LastName;
                char *Birthday;  // in the format of 12/30/1978
                int  id;
        };


        PersonalData PersonOne;

        // Populate PersonOne with data
        PersonOne.FirstName = "John";
        PersonOne.LastName = "Doe";
        PersonOne.Birthday = "12/30/1978";
        PersonOne.id = 5;

        // Populate PersonTwo with data
        PersonTwo.FirstName = "Matt";
        PersonTwo.LastName = "Thompson";
        PersonTwo.Birthday = "15/20/1958";
        PersonTwo.id = 3;


thanks in advance!
Are you inputting people from a file? How are you adding them ?
This is how i add people:

1
2
3
4
5
6
PersonalData PersonOne;
// Populate PersonOne with data
        PersonOne.FirstName = "John";
        PersonOne.LastName = "Doe";
        PersonOne.Birthday = "12/30/1978";
        PersonOne.id = 5;



And if i wanna add more people, id add some code.... PersonTwo,... PersonThree....
Last edited on
If you are in control of the input for this program, you should create an input file with the names and information in the order you want to read them in. Then you can read it into an array. This will bring everything into a nice orderly place then you can use basically the same sort function I gave you above. Otherwise I think (I think) it will be a pain to have all of the people separated all over the place. It's going to make it hard to sort.
Look try this for an input function just create a file and order the input
John
Doe
12/...etc.
You could literally just modify this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Function to get info from file.
void readData(ifstream &fin, infoType tele[], int& size)
{	
	int i = 0;
	while(!fin.eof())
   {
		fin >> tele[i].fname;
		fin >> tele[i].lname;
		fin.get();
		getline(fin,tele[i].streetAdd,'\n');
		getline(fin,tele[i].cityStateZ,'\n');
		getline(fin,tele[i].phone,'\n');
	i++;
   }
		size = i;
}

I needed the size = i but if you know how many you are bringing in you could do without.
Then once you have this into an array just use the sort function I gave you. The only thing you actually have to do is create the array to be read into.
done
Last edited on
You got it solved?
Topic archived. No new replies allowed.