Thread: Sorting Program... :(

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you store your different player structs in an STL container such as a list or a vector you can then use the sort function on that container and automatically sort them based on whatever criteria you want. For instance to sort in ascending order by score_avg you could do:
    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;  // Edit: Forgot this
    
    // This is a simplified struct just to illustrate my point...
    
    struct team
    {
        float score_avg;     // Average Points player makes per game
    };
    
    // Define a function to compare the score_avg data members of two
    // different team structures.  You will need to create two additional
    // comparison functions to compare based on the name and the
    // grade point average.
    
    bool score_comp( const team& t1, const team& t2)
    {
        return t1.score_avg < t2.score_avg;
    }
    
    int main()
    {
        vector<team> players;    // Hold a whole mess of team structures
        vector<team>::iterator it;
        team player;    // Hold a single players data
    
        // Add some team structs to the vector.  You will need to replace
        // this with your code to open your file and read all the data into
        // your team struct and push it into the vector.
    
        player.score_avg = 54.8;
        players.push_back(player);    // Players contains 1 team struct w/ value 54.8
        player.score_avg = 47.2;
        players.push_back(player);    // Players contains 2 team structs w/ values 54.8, 47.2
        player.score_avg = 99.8;
        players.push_back(player);    // Players contains 3 team structs w/ values 54.8, 47.2, 99.8
    
        // Now display the unsorted vector...
        // This will output 54.8, 47.2, 99.8 on individual lines
    
        for( it = players.begin(); it != players.end(); ++it )
            cout << it->score_avg << endl;
    
        // Now sort the vector using the score_comp function
        // Replace score_comp with name of your other comparison
        // functions to sort the data in players vector differently
        // based on your needs.
    
        sort(players.begin(),players.end(),score_comp);
    
        // Now display the vector again, should be sorted...
        // This will output 47.2, 54.8, 99.8 on individual lines
    
        for( it = players.begin(); it != players.end(); ++it )
            cout << it->score_avg << endl;
    
        return 0;
    
    }
    This will give you an idea of how easy it can be to sort data once it is in a vector. You can sort in reverse order based on the score_avg data member just by changing the '<' to '>' in the score_comp function.
    Last edited by hk_mp5kpdw; 11-05-2003 at 07:23 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM