#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11

    Not sure how to sort an array..


    I've been working with C++ for a short time now and I've been working on a small project that stores peoples first name, last name and age. Everything works and I can get all the data to print to the screen.

    My question is, if I put a choice for the user to sort the list (by name or by age), how would I sort the items in the array?

    I'm using a class to get the names etc. Here's where I'm sending it to the terminal

    PHP Code:
        for (int i 0indexi++)
        {
            
    displayData(nds[i]);
        }
        return 
    0
    Can I enter something there or is it a little more complicated than that? I can post all the code if needed.

    Thanks in advance!
    Last edited by dcj1978; September 18th, 2003 at 03:24 PM.
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    You may need to post more code than that. The data types and class definitions would be a good start, ans well as the declaration of nds.

    You could simply use the qsort() function from the standard C library. You would need to create a class compare helper function and pass a pointer to it to qsort().

    Before your data output loop:
    Code:
    qsort( nds, index, sizeof(*nds), myCompareFn ) ;
    Where the myCompareFn() is the compare function you must write yourself. It must take two pointers to your data class, and return <0, 0, or >0 depending on the comparison. You may need to make this function a friend of your data class in order to allow it access to private members. You could make it a static member of the class, which may be tidier. qsort() is declared in <cstdlib> (or <stdlib.h>). For details of qsort() see http://www.cplusplus.com/ref/cstdlib/qsort.html.

    Clifford
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11
    The code has actually changed a bit since my first post. I'm not exactly sure what all you would need so, here's the complete code.

    PHP Code:
    #include <iostream>
    #include <string>
    #include <algorithm>

    using namespace std;

    int main()
    {
        
    //declare arrays
        
    string first[2] = {""""};
        
    string last[2]   = {""""};
            
        
    string firstName ""
        
    string lastName "";
                
        
    int x 0;
        
    int counter 0;
            
        while (
    2)
        {
            
            
    cout << "Enter first name: ";
            
    cin >> firstName;
            
    cout << "Enter last name: ";
            
    cin >> lastName;
            
            
            
    first[counter] = firstName;
            
    last[counter] = lastName;
             
            
    1;

            
    counter counter++;
        }  
    //end while

    /* Need to allow the user to select how to sort the output here. 
     I can give them the choice, but actually sorting the array is where i'm stuck */

        
    for (02x++)
        {
         
         
    cout << first[x] << last[x] << endl;
          }
        
    system("PAUSE");

        return 
    0;

    Thanks for the reply!
    Last edited by dcj1978; September 20th, 2003 at 07:20 PM.

IMN logo majestic logo threadwatch logo seochat tools logo