how to do it depends on what type of list you are using and what methods are available in apstring.
If you are using a list which is an instance of the STL list class, then you can use the generic STL function called sort() or a list member function called sort(), I belive. If you have an aplist class it might have similar functionality.
Otherwise, you can write your own sorting function. I prefer to sort at time of insertion. But you could develop your own "bubble sort" to work with a list too, I suppose. It might look something like this (code not compiled or tried):
Code:
//use a node called current nd previous to help keep your place
node * current;
node * previous;
//if your list's head node is not already a "blank" holding node, make it so.
node * blank = new node;
blank->next = head;
head = blank;
delete blank;
blank = NULL;
//likewise create a blank end node
node * end = new node;
end->next = NULL;
//and put it in place
current = head->next;
while(current-next != NULL)
{
current = current->next;
}
current->next = end;
//now bubble the "largest" node capital to the spot before end
while(end != head->next->next)//need two nodes to compare!
{
//each time through list (re)set current and previous
current = head->next;
previous = head;
while(current->next != end) //need two nodes to compare!
{
//compare nodes using the > operator if available in apstring
if(current->capital > current->next->capital)
//if > not overloaded in apstring use strcmp()
//if(strcmp(current->capital, current->next->capital) > 0)
{
//switch nodes current and current-next to get bubble effect
blank = current->next->next;
previous->next = current->next;
current->next->next = current;
current->next = blank;
}
else
{
current = current->next;
}
}
//advance end one to the left
end = current->next;
}