Thread: Sorting problem

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    Sorting problem

    Hi everyone!

    If anybody could tell me why this sort is not working I would greatly appreciate it.

    I'm trying to sort by last name, first name and middle initial. I have the last name sorted, but it seems to stop there. I've looked at this and tried so many different things for so long I think I may just be missing something silly.

    Code:
    void Sort(vector<Names>& Access, vector<Entry>& Process, int& i)
    // Pre condition: Add function has been accessed
    // Post condition: Returns sorted Phone book
    {
    	int  j;
    	struct Name temp[20];
    	struct Phone_Book_Entry temp2[20];
    	cout << Process.size();
    
    	for(i=0; i<=Process.size(); i++)
    	{
    		for(j=i+1; j<=Process.size(); j++)
    		{
    
    			if (Access[i].lastName > Access[j].lastName)
    			{
    
    				temp[i] = Access[i];
    				Access[i] = Access[j];
    				Access[j] = temp[i];
    				
    				temp2[i] = Process[i];
    				Process[i] = Process[j];
    				Process[j] = temp2[i];
    			
    				if (Access[i].firstName > Access[j].firstName)
    				{
    					temp[i] = Access[i];
    					Access[i] = Access[j];
    					Access[j] = temp[i];
    				
    					temp2[i] = Process[i];
    					Process[i] = Process[j];
    					Process[j] = temp2[i];
    					
    					if (Access[i].middleIni > Access[j].middleIni)
    					{
    						temp[i] = Access[i];
    						Access[i] = Access[j];
    						Access[j] = temp[i];
    				
    						temp2[i] = Process[i];
    						Process[i] = Process[j];
    						Process[j] = temp2[i];
    					
    					} // End 3rd if
    				
    				} // End 2nd if
    
    			} // End 1st if
    
    		} // End for j
    	
    	} // End for i
    
    } // End Sort function

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I'm a little unsure of what you are trying to do. Are you sorting by last name and if they are equal, THEN sort by first name, and if that is equal, THEN sort by middle name? Right now its set up to check if one last name is greater than another, and if so, then swap them AND check to see if the first name is greater, if so then etc. I don't think this is what you want! If what you want is what I stated then you need to change it to something like:
    Code:
    if (access[i].lastname>assess[j].lastname)
    {
      swap 'em
    }
    else if (access[i].lastname==access[j].lastname)
    {
       if (access[i].firstname>access[j].firstname)
       {
          swap'em
       }
       else if (access[i].firstname==access[j].firstname)
       {
         if (acess[i].middle==access[j].middle)
         {
            swap'em
         }
       }
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Thanks PJ!!! That did the trick!!!

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    No prob! Another way if you want to really shorten your code would be to write it in one if statement like so:
    Code:
    if (firstname1>firstname2 || (firstname1==firstname2 && lastname1>lastname2) ||
       (firstname1==firstname2 && lastname1==lastname2 && middle1>middle2))
      {
         swap'em
      }

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    Great plan!

    Thanks for that advice, wow, what a difference!!! I have trouble with condensing my code, so this is absolutely welcomed information. I just need to learn how to think in this manner. I'm usually a quick learner, but for some reason I blank out on the if statements.

    Thanks for the great advice!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problem?
    By audinue in forum C Programming
    Replies: 5
    Last Post: 01-06-2009, 02:16 PM
  2. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  3. What is problem about the sorting?
    By Mathsniper in forum C Programming
    Replies: 2
    Last Post: 04-17-2005, 07:00 AM
  4. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 01:44 PM