char *	crap = type.ToNewCString(); if (crap) { delete []crap; crap = nsnull; }
XULSortServiceImpl::SortTreeChildren(nsIContent *container, PRInt32 colIndex, sortPtr sortInfo, PRInt32 indentLevel){	PRInt32			childIndex = 0, numChildren = 0, nameSpaceID;        nsCOMPtr<nsIContent>	child;	nsresult		rv;	if (NS_FAILED(rv = container->ChildCount(numChildren)))	return(rv);	nsCOMPtr<nsISupportsArray> childArray;	rv = NS_NewISupportsArray(getter_AddRefs(childArray));	if (NS_FAILED(rv))	return(rv);	for (childIndex=0; childIndex<numChildren; childIndex++)	{		if (NS_FAILED(rv = container->ChildAt(childIndex, *getter_AddRefs(child))))	break;		if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID)))	break;		if (nameSpaceID == kNameSpaceID_XUL)		{			nsCOMPtr<nsIAtom> tag;			if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag))))	return rv;			if (tag.get() == kTreeItemAtom)			{				childArray->AppendElement(child);								// if no pos is specified, set one				nsAutoString	pos;				if (NS_OK != child->GetAttribute(kNameSpaceID_None, kNaturalOrderPosAtom, pos))				{					nsAutoString	zero("0000");					pos = "";					pos.Append(childIndex+1, 10);					if (pos.Length() < 4)					{						pos.Insert(zero, 0, 4-pos.Length()); 					}					child->SetAttribute(kNameSpaceID_None, kNaturalOrderPosAtom, pos, PR_FALSE);				}			}		}	}	unsigned long numElements = childArray->Count();	if (numElements > 0)	{		nsIContent ** flatArray = new nsIContent*[numElements];		if (flatArray)		{			// flatten array of resources, sort them, then add as tree elements			unsigned long loop;		        for (loop=0; loop<numElements; loop++)		        {				flatArray[loop] = (nsIContent *)childArray->ElementAt(loop);			}			/* smart sorting (sort within separators) on name column */			if (sortInfo->sortProperty == kNC_Name)			{				PRInt32			startIndex=0;				for (loop=0; loop<numElements; loop++)				{					nsAutoString	type;					if (NS_OK == flatArray[loop]->GetAttribute(kNameSpaceID_None, kRDF_type, type))					{											char *	crap = type.ToNewCString();						if (crap)						{							delete []crap;							crap = nsnull;						}						if (type.EqualsIgnoreCase(kURINC_BookmarkSeparator))						{							if (loop > startIndex+1)							{								nsQuickSort((void *)&flatArray[startIndex], loop-startIndex, sizeof(nsIContent *),									inplaceSortCallback, (void *)sortInfo);								startIndex = loop+1;							}						}					}				}				if (loop > startIndex+1)				{					nsQuickSort((void *)&flatArray[startIndex], loop-startIndex, sizeof(nsIContent *),						inplaceSortCallback, (void *)sortInfo);					startIndex = loop+1;				}			}			else			{				nsQuickSort((void *)flatArray, numElements, sizeof(nsIContent *),					inplaceSortCallback, (void *)sortInfo);			}			RemoveAllChildren(container);			if (NS_FAILED(rv = container->UnsetAttribute(kNameSpaceID_None,			                        kTreeContentsGeneratedAtom,			                        PR_FALSE)))			{				printf("unable to clear contents-generated attribute\n");			}						// insert sorted children						numChildren = 0;			for (loop=0; loop<numElements; loop++)			{				container->InsertChildAt((nsIContent *)flatArray[loop], numChildren++, PR_FALSE);			}			// recurse on grandchildren			for (loop=0; loop<numElements; loop++)			{				container =  (nsIContent *)flatArray[loop];				if (NS_FAILED(rv = container->ChildCount(numChildren)))	continue;				for (childIndex=0; childIndex<numChildren; childIndex++)				{					if (NS_FAILED(rv = container->ChildAt(childIndex, *getter_AddRefs(child))))						continue;					if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID)))	continue;					if (nameSpaceID == kNameSpaceID_XUL)					{						nsCOMPtr<nsIAtom> tag;						if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag))))							continue;						if (tag.get() == kTreeChildrenAtom)						{							SortTreeChildren(child, colIndex, sortInfo, indentLevel+1);						}					}				}			}			delete [] flatArray;			flatArray = nsnull;		}	}        for (int i = childArray->Count() - 1; i >= 0; i--)        {        	childArray->RemoveElementAt(i);        }	return(NS_OK);}