Help with Sorting

This is a discussion on Help with Sorting within the C++ Programming forums, part of the General Programming Boards category; I am having trouble on my sort. I am trying to sort First and Last Name (in ABC order), and ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    11

    Help with Sorting

    I am having trouble on my sort. I am trying to sort First and Last Name (in ABC order), and for every name there is the age, city, state, etc.. Obviously when I sort the Names, I have to get the age, city, state, etc.. to go along with it. I have managed to get age and zipcode to switch along with the names, but I dont know how to change the city to go along with the name because it is a char. How would I do this?

    I know my code is really rough, and there are better ways to do the whole code, but what is the most simplest way possible to get the city to change along with the First and Last name? Thanks!

    This is the WHOLE Code:
    http://rafb.net/p/1uDyjo27.html

    This is the section I am working on now:
    Code:
    void searchfull()
    {
    	int j;
    	char tempf[25][25],templ[25][25],tempr[25][25],tempq[25][25];
    	int tempn[25],tempx[25];
    	for (j=1; j<=u-1; j++)
    {
    		for (I=1; I<=u-1; I++)
    	{               namecmp=strcmpi(namel[I], namel[I+1]);
     
    			if(namecmp > 0)
    			{
    			   strcpy(tempf[I],namef[I]);
    			   strcpy(templ[I],namel[I]);
     
    			   strcpy(tempr[I],city[I]);
    			   tempn[I]=zipcode[I];
    			   tempx[I]=age[I];
     
    			   strcpy(namef[I], namef[I+1]);
    			   strcpy(namel[I], namel[I+1]);
    			   strcpy(city[I], city[I+1]);
    			   zipcode[I]=zipcode[I+1];
    			   age[I]=age[I+1];
    			   strcpy(namef[I+1],tempf[I]);
    			   strcpy(namel[I+1],templ[I]);  
    			   strcpy(city[I], tempr[I+1]);
    			   zipcode[I+1]=tempn[I];
    			   age[I+1]=tempx[I];
    			}
    			else if(namecmp==0)
    			{
    			  namecmp=strcmpi(namef[I], namef[I+1]);
    			  if(namecmp> 0)
    			  {
                               strcpy(tempf[I],namef[I]);
    			   strcpy(tempr[I],city[I]);
    			   strcpy(templ[I],namel[I]);
    			   tempn[I]=zipcode[I];
                               tempx[I]=age[I];
    			   strcpy(namef[I], namef[I+1]);
    			   strcpy(namel[I], namel[I+1]);
    			   strcpy(city[I],city[I]);
    			   zipcode[I]=zipcode[I+1];
    			   age[I]=age[I+1];
    			   strcpy(namef[I+1],tempf[I]);
    			   strcpy(namel[I+1],templ[I]);
    			   strcpy(city[I],tempr[I]);
    			   zipcode[I+1]=tempn[I];
    			   age[I+1]=tempx[I];
     
     
     
    			  }
    			}
    	}
    }
    If you want to talk to me one on one, I am available on AIM at Chronotrigga05 right now . Or you can reply down here, Ill reply back.
    Last edited by Chronotrigga; 05-23-2007 at 06:34 PM.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    What you need is 1) to learn about classes and (more generally) OOP and 2) stop using C strings and start using std::string string.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    Create a struct for all your data, not a bunch of parallel arrays.
    Code:
    struct info {
        char namel[20];
        char namef[20];
        char city[10];
        int zipcode;
        int age;
    };
    info myArr[25];  // or however many you have
    Keeping records which are supposed to be together actually together becomes so much easier.

    Then you'll be able to write rather more compact
    Code:
    if(namecmp > 0)
    {
        info temp = myArr[i+1];
        myArr[i+1] = myArr[i];
        myArr[i] = temp;
    }
    But as Desolation says, std::string fname; would be much preferred.


    > namecmp=strcmpi(namel[i], namel[I+1]);
    Two global variables, both called 'i', one in lower case and one in upper case - UGH!
    Anyway, to use the array, it would be
    Code:
    namecmp=strcmpi(myArr[i].namel, myArr[I+1].namel);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    11
    For:
    Code:
    if(namecmp > 0)
    {
        info temp = myArr[i+1];
        myArr[i+1] = myArr[i];
        myArr[i] = temp;
    }
    Do I have to make a new struct for temp?

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Forget about those ugly C-style strings and use std::string.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    What's wrong with the struct you've got?

    It's the same type as elements of your array, so assignment does exactly what you would want it to do (in this case).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    11
    Well, my project is due soon like tommorow, but I am willing to give std::string a shot to learn it; can you give me an example of how to use it or lead me to a site?

    But even if C-style strings are ugly, I at least know how to 'kind of' do it. Anyway, I was talking about info temp; if I have to make a new struct info for it or not.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    No you don't.

    It it were an array of int, and int temp; would there be an issue?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    11
    Yes, there would be if it was an int of temp. Anyway; it runs but it doesn't sort ABC.. Not sure:

    Code:
    void searchfull()
    {
    	int j;
    	char tempf[25][25],templ[25][25],tempr[25][25],tempq[25][25];
    	int tempn[25],tempx[25];
    	for (j=1; j<=u-1; j++)
    {
    		for (I=1; I<=u-1; I++)
    	{               namecmp=strcmpi(MyArr[I].namel[I], MyArr[I+1].namel[I]);
    
    			if(namecmp > 0)
    			{
    			   info temp=MyArr[I+1];
    			   MyArr[I+1]=MyArr[I];
    			   MyArr[I]=temp;
    			}
    			else if(namecmp==0)
    			{
    			  namecmp=strcmpi(MyArr[I].namef[I], MyArr[I+1].namef[I+1]);
    			  if(namecmp> 0)
    			  {
    			  info tempi=MyArr[I+1];
    			   MyArr[I+1]=MyArr[I];
    			   MyArr[I]=tempi;
    
    
    
    			  }
    			}
    	}
    }
     
     
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM

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