Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sorting an array of a class  by last names..

Posted on 2003-12-01
9
Medium Priority
?
429 Views
Last Modified: 2010-04-17
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);

}

0
Comment
Question by:kaladran1974
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
9 Comments
 
LVL 22

Accepted Solution

by:
cookre earned 500 total points
ID: 9854052
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
 

Author Comment

by:kaladran1974
ID: 9854361
The problem is they are not comparing, the list does not change..  made some changes to it and now i just get crashes.
0
 
LVL 22

Expert Comment

by:cookre
ID: 9859237
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:pv12206
ID: 9864324
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
 
LVL 22

Expert Comment

by:cookre
ID: 9889037
How's it going?
0
 

Expert Comment

by:pv12206
ID: 9891290
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Introduction to Processes
Starting up a Project

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question