Sorting an array of a class by last names..

Im looking for some help or a good website with sorting information.  Im not going to post all my code. I had no problem sorting the array by voterid. Here is code i used for that:

void sortid(voter v1[], int size)
 {
    bool doMore;
    do {
        doMore = false;  // assume this is last pass over array
        for (int i=0; i<size-1; i++) {
            if (v1[i].idnumber > v1[i+1].idnumber) {
                // exchange elements
                voter temp = v1[i]; v1[i] = v1[i+1]; v1[i+1] = temp;
                doMore = true;  // after exchange, must look again
            }
        }
    } while (doMore);
}

The problem now is that the array needs to be sorted by last name. This is where im having problems. Not going to post all of code.

Class Declaration

class voter
{

friend void sortid(voter v1[], int size); <--- this is for sortby id number
friend void sortlastname(voter v1[], int size); <----sort by lastname


private:      
      int  idnumber;
      char lastname [25];
      char firstname[25];
      char party         [2];
      int  birthyear;
      int  currentage;

public:
      voter();
      voter(int,char[], char[], char[], int, int);
      voter(const voter&);
      void Add();
      void Edit();
      void operator = (const voter&);
      void print(int);
      void disksave(ofstream& outfile);
      ~voter();

};

and now here is the code i attempt to use for sorting by lastname.

void sortArray (voter n[], int size) {

    char temp[25; // temporary buffer for strings
    int swap;
   
    do {
        swap = 0;
        for (int count = 0; count < (size- 1); count++) {
            if (strcmp(n[count].lastname, n[count + 1].lastname) > 0) {
                strcpy(temp, n[count].lastname); // temp = n[count.lastname]
                strcpy(n[count].lastname, n[count + 1].lastname); // n[count] = n[count + 1]
                strcpy(n[count + 1].lastname, temp); // n[count+1].lastname = temp
                swap = 1;
            };
        };
    } while (swap != 0);

}

kaladran1974Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cookreCommented:
You don't say what the problem is, so here are some guesses:

* strcmp is case dependant, use stricmp for case insensitive
* strcmp presumes single byte characters, use wcscmp or mbscmp for wide- or multi-byte
* strcmp is ascii collating and locale independant.  If locale matters, use strcoll
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kaladran1974Author Commented:
The problem is they are not comparing, the list does not change..  made some changes to it and now i just get crashes.
0
cookreCommented:
Just crashing sure sounds like you're trashing something.

At the top of sortArray I'd dump out 'size' and 'n' to make sure good stuff is coming in.  For example, if size>25 you're in dep dookie.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

pv12206Commented:
Selection sort should solve the problem. You have the idea here but I think instead of use do while loop, use both for loop to keep the current position of the index you have not sorted.

The algorithm:

for (index =0; index < size; index++)
{
   step 1: find the location of the smallest element first. You don't need to swap everytime you find the smaller element.
   step 2: Swap the smallest  element with n[index]. there's also a swap function predefine for string.

}

The Code should look like:
void sortArray (voter n[], int size) {
  int i, j;
  int min;  
  for ( i=0; i < size -1; i++ ) { //size- 1 because the last  element is automaticly in order
    min = i; // min is the index of smallest element that compare to the rest.
    /*this loop find the smallest element and */
    for (j = i+1; j < size; j++)
       if ( strcmp(n[j].lastname, n[min].lastname < 0 )
           min = j;
   /*after this for loop min should be the index  of smallest element
    n[i].lastname.swap(n[min].lastname);
  }
}

Ok this should work. good luck
0
cookreCommented:
How's it going?
0
pv12206Commented:
I'm miss that. I relize that what you're sorting here is the array of voter by lastname. not sorting the last name. So the swap code should be:

voter temp; //holder for swaping
....
..
....
temp = n[i];
n[i] = n[min];
n[min] = temp;

 Ofcourse, there is no "= " sign for type voter yet. In your class definition you need to have an Operator Overloading function to define the meaning of the " = " sign for voter type.
 I hope this will help you to solve the problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.