# Abot Arrary Sorting

• 04-04-2006
FeNCinGeR
Abot Arrary Sorting
Can somebody help and say what is wrong with my problem - I try to sort arrary names, but something wrong...

Code:

```#include <iostream>          using namespace std; struct persona                  {   char vards[15];      // name        char uzvards[15];  // Surname          int gads;                // birth year   } pers[3];   void druka(persona pers[]); void pecv(persona pers[]); void pecu (persona pers[]); int main() {  for ( int i=0; i<3; i++ )  {  cout<<"Ievadiet vardu: "; // name  cin>>pers[i].vards;  cout<<"Ievadiet uzvardu: "; // surname  cin>>pers[i].uzvards;  cout<<"Ievadiet dzimsanas gadu: "; // birth year  cin>>pers[i].gads;  }     cout<<"Saraksts ievadita seciba:"<<endl;  druka(pers);        cout<<"Saraksts sakartots pec gadiem:"<<endl;  pecv(pers);  druka(pers);   cout<<endl; system("pause"); return 0; }     void pecv (persona pers[])     {     for(int i=0; i<3 - 1; i++)         for(int j=0; j<3 - 1; j++)           if (pers[j].gads>pers[j+1].gads)           {           int tmp = pers[j].gads;           pers[j].gads = pers[j+1].gads;           pers[j+1].gads = tmp;           }     }     void pecu (persona pers[]) // This doesn't works... It's sorting by surname     {     char temp[15];     for(int i=0; i<3 - 1; i++)         for(int j=0; j<3 - 1; j++)           if (pers[j].uzvards>pers[j+1].uzvards)           {           temp = pers[j].uzvards;           pers[j].uzvards = pers[j+1].uzvards;           pers[j+1].uzvards = temp;           }     }     void druka (persona pers[]) // print out function     {     for ( int i=0; i<3; i++ )     cout<<"Vards: "<<pers[i].vards<<"\tUzvards: "<<pers[i].uzvards<<"\tdz. "<<pers[i].gads<<" g."<<endl;     }```
It's in Latvian main things - uzvards - surname!

I already made that this programm sorts people who you wrote in by birth year ( LV - dzimsanas gads )

Can somebody edit section -> void pecu (persona pers[]) ( Sorting by surname )
• 04-04-2006
hk_mp5kpdw
Have you considered using the std::sort function? Also, you should be using std::string's instead of char arrays.

Your current sorting functions only attempt to sort the data in the birthyear and surname fields, they do not affect the remainder of the data for the structs you are working with. For example, you may sort the birthyears, but the original names associated with those birthyears may belong with a different array index.

Code:

```struct persona                  {   char vards[15];      // name        char uzvards[15];  // Surname          int gads;                // birth year   } pers[3]; ... void pecu (persona pers[]) // This doesn't works... It's sorting by surname     {     char temp[15];     for(int i=0; i<3 - 1; i++)         for(int j=0; j<3 - 1; j++)           if (pers[j].uzvards>pers[j+1].uzvards)           {           temp = pers[j].uzvards;           pers[j].uzvards = pers[j+1].uzvards;           pers[j+1].uzvards = temp;           }     }```
Comparisons using character array's and assignment of character arrays don't work like you may think they do. This is a big reason your sort function does not work. You need to be using the strcmp and strcpy functions to perform these tasks. This is why it is much better to use a string which does work like this.

An example using std::strings and the std::sort function.
Code:

```#include <algorithm> #include <string> #include <iostream> using namespace std; struct persona {     string vards;     string uzvards;     int gads; }; class sort_by_surname { public:     inline bool operator()(const persona& per1, const persona& per2)     {         // Can do this with strings but not with char arrays.         return per1.uzvards < per2.uzvards;     } }; int main() {     persona pers[3];     // Get data into pers array.     ...     // Sort the pers array by surname.     sort( pers, pers+3, sort_by_surname() );     return 0; }```