Thread: sorting objects

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    sorting objects

    hey all,

    how do you sort objects? i can sort the actual object just fine, but doesn't that take up a lot of memory, passing the really big objects back and forth? i tried pointers, but can't find the right syntax. let's say i have the following, or thereabouts:

    Code:
    class test 
    {
    	int x, y, z
    	//.... constructors and so on
    
    
    int main(){
    
    test t1(1),
    	 t2(2),
    	 t3(3);//i want to eventually sort the object by the parameter
    
    //... function to sort test's objects
    what's the correct syntax for sorting t1, t2, t3?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Do you mean sort tests stored within a container, or sort the values within one instance of test?

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    well, i don't know what a container is right now, but this is exactly what i need: instances that need to be sorted at runtime to be used, in that order, later on in the program.

    imagine the following instances are baseball players with values for pitching, chatching, and so on. i need the user to sort their batting order. so what i wanted to do was to place pointers in a que rather than objects:

    Code:
    	Player h1(12,3,3,0,0,4,2,3,"Alby Abe", 1);
    	Player h2(14,1,1,1,1,1,1,1,"Boss",2);
    	Player h3(1,3,3,0,0,4,2,3,"Chuck up", 3);
    	Player h4(7,1,1,1,1,1,1,1,"Davy",4);
    	Player h5(5,3,3,0,0,4,2,3,"Eirn", 5);
    	Player h6(0,1,1,1,1,1,1,1,"Fred",6);
    	Player h7(88,3,3,0,0,4,2,3,"Godfry", 7);
    	Player h8(6,1,1,1,1,1,1,1,"Hippe",8);
    	Player h9(15,3,3,0,0,4,2,3,"Ian", 9);
    	Player h10(27,1,1,1,1,1,1,1,"Jack",0);
    	Player h11(16,3,3,0,0,4,2,3,"Killer", 0);
    	Player h12(22,1,1,1,1,1,1,1,"Lane",0);
    	Player h13(3,3,3,0,0,4,2,3,"Matty", 0);
    	Player h14(11,1,1,1,1,1,1,1,"Ned",0);
    	Player h15(20,3,3,0,0,4,2,3,"Oris", 0);
    (sorry about any typos, i cut my fingers this week and am having trouble typing )

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Sorry, I'm still a bit confused. Do you just want to create a list of players entered by a user or use some pre-defined sorting function to insert the players into a list in some order, such as one of the attributes the players have?

  5. #5
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    a little bit of both. i want the user to sort the above instances to his liking. then i want the program to store them, in that order for use latter uin the program. i'm having a difficult time comunicating today, aren't i?

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    24
    i think I know what you want to do but have no idea how to go about doing it.
    #include <iostream.h>
    int main()
    {
    cout<<"Hello";
    return 0;
    }

  7. #7
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    well, i didn't think it would be difficult at all. can't i just point to a object?

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    A simple example -

    Code:
    #include <iostream> 
    #include <vector> 
    #include <algorithm>
    
    using namespace std;
    struct player
    {
    	int pitching;
    	player (int x):pitching(x){}
        
    	bool operator <(const player& rhs)const
    	{
    		return pitching < rhs.pitching;
    	}
    
    	friend ostream& operator <<(ostream& os,const player& p)
    	{
    		return os << p.pitching;
    	}
    
    
    };
    
    int main() 
    { 
    	player one(55);
    	player two(125);
    	player three(81);
    
    	vector<player> players;
    
    	players.push_back(one);
    	players.push_back(two);
    	players.push_back(three);
    
    	sort(players.begin(),players.end());
    
    	
    	copy(players.begin(),players.end(),ostream_iterator<player>(cout," "));
    
    	
    	return 0;
    }
    If you want to just use pointers you can create a vector of player pointers rather than player objects (you'd have to create a global comparison function that accepts pointers that you can plug into std::sort). A different container (std::list, std::set, etc) may be more suitable depending on how you want to create and maintain the container, and whether you need to frequently re-sotr.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM