Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 03-11-2009
Member
 
Join Date: Aug 2009
Posts: 51
Sorting a list of type class

I have declared a list from the STL. This is a list of the type of class. But I am unable to work out my operator:

Code:
class Node
{
public:
string label;
list<Node*> child;
bool operator< (const Node *c1)
{
 return label < c1->label;
}
Node (string text)
{
 label=text;
}
void addChild(string text)
{
 Node* newNode = new Node (text);
 child.push_front(newNode);
}
void display(int NbTab)
{
 if (!child.empty())
 {
  child.sort();
  insertTab(NbTab);
  cout<<label<<endl;
  list<Node*>::iterator it;
  for (it=child.begin(); it!=child.end(); ++it)
  {
   (*it)->display(NbTab+1);
  }
 }
 else
 {
  insertTab(NbTab);
  cout<<label<<endl;
 }
}
};
int main(void)
{
Node* level1=new Node ("Level1" );
Node* level4=new Node ("Level4" );
level1->addChild("Level20" );
level1->addChild("Level22" );
level1->addChild("Level23" );
level1->addChild("Level21" );
level1->child.front()->addChild("test2" );
level1->child.front()->addChild("test1" );
level1->child.front()->addChild("test3" );
level1->display(0);
level1->child.sort();
level1->display(0);
return 0;
}
Here is the return to the console:
Quote:
Niveau1
Level20
Level22
Level23
Level21
test2
test1
test3
Level1
Level20
Level22
Level23
Level21
test2
test1
test3
Press any key to continue
I display my list, everything is OK and it is in disorder (order in which I returned). I then re-sort it but still the order remains the same. Could someone tell me what I miss? Can you help me how to sort a list that is of type class?
Reply With Quote
  #2  
Old 03-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
Re: Sorting a list of type class

How does nothing happens? It sorts according to your pointers, as you probably ask him.

A list of pointers will be sorted with the order defined on pointers, not objects points if you do not explicitly specify the comparison function to use.
Reply With Quote
  #3  
Old 03-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: Sorting a list of type class

You can call out to passing it as argument may be, its the predicate that will make the comparison as you want between nodes

Code:
bool pred( const Node *l, const Node *r )
{
return l->label < r->label;
}
// ...
level1->child.sort(pred);
Incidentally, you note that your sort is not deep, you can sort the child of a node, not the small child.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 03-11-2009
Member
 
Join Date: Aug 2009
Posts: 51
Re: Sorting a list of type class

@ const: How can I specify this function to sort my list as per my requirement?

@ opaper: thank you for enlightening me, I confess that I passed but I think you've noticed

I already try the same thing, the problem is that I can not use it in my class node (in any case I failed)
Reply With Quote
  #5  
Old 03-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: Sorting a list of type class

The predicate should not be a member function of your class or it is necessary that he be declared as static member. Always see what you try and what errors do you get, it will be quicker to help.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Sorting a list of type class"
Thread Thread Starter Forum Replies Last Post
Confused about the List class Jagruti23 Software Development 5 05-03-2010 04:17 PM
Abstract class unable to implement List<T> MAHESA Software Development 5 02-03-2010 09:00 PM
Creating a vector type of a class Xmen Software Development 5 24-02-2010 03:38 AM
Problem in List-style-type property Juany Software Development 5 23-02-2010 04:17 PM
Is there a master list of Microsoft Windows Server 2003 services that will also list what the default startup type? Spin Windows Server Help 1 21-09-2008 09:05 AM


All times are GMT +5.5. The time now is 11:07 PM.