Thread: Abot Arrary Sorting

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Ventspils, Latvia
    Posts
    2

    Question 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 )

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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;
    }
    Last edited by hk_mp5kpdw; 04-04-2006 at 08:41 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM