Ive been working for hours and days at this, so if you have any idea what im talking about, please help!
Overview: I have a number of objects of type CList which are linked together using pointers (like a linked list but with objects rather than structs). I need to sort the list by quantity .
What Ive tried: Putting the pointers to the objects in a RPointerArray. Trying to bubble sort this. Then casting this back to the original pointers which join the objects! Sounds complex i know, but ive tried all the easy options and its just evolved to this.
What i have so far...
General Initilasation where pointers is the RPointerArray
Then the sort itself
TInt j, k;
TBool exchange_made = ETrue;
// Initialize pointer array
probe = Head;
for (k = 0; k < (NodeCount + 1); k++)
probe = probe->Next;
k = 0;
The assigning the list back to the original pointers
while ((k < NodeCount) && exchange_made)
exchange_made = EFalse;
for (j = 0; j < (NodeCount+1) - k; j++)
if (pointers[j]->Quantity > pointers[j + 1]->Quantity)
CList* temp = static_cast<CList*> (pointers[j]); // Swap pointers
CList* temp2 = static_cast<CList*> (pointers[j+1]);
pointers.Remove( j );
User::LeaveIfError( pointers.Insert(temp2, j) );
pointers.Remove( j+1 );
User::LeaveIfError( pointers.Insert( temp, j+1) );
exchange_made = ETrue;
I think the problems somewhere within the bubble sort bit itself, and moving the pointers arround. If you can see fault with it please tell me, or if you know of a better way to sort the pointers around, please let me know. (and ive tried not using the pointerarray and assigning CurrentPtr->Next = temp, but that didnt seem to work!)
TInt ptrCount = pointers.Count();
Head = static_cast<CList*> (pointers);
Head->Next = static_cast<CList*> (pointers);
CurrentPtr = Head->Next;
for (TInt i = 1; i < ptrCount-1; i++)
CurrentPtr->Next = static_cast<CList*> (pointers[i]);
CurrentPtr = CurrentPtr->Next;