nsCOMPtr<nsIRDFResource> resource; gXULUtils->GetElementResource(child, getter_AddRefs(resource));
nsCOMPtr<nsIRDFResource>	resource; nsXULContentUtils::GetElementResource(child, getter_AddRefs(resource));
XULSortServiceImpl::SortTreeChildren(nsIContent *container, sortPtr sortInfo, PRBool merelyInvertFlag){	PRInt32			childIndex = 0, loop, numChildren = 0, numElements = 0, currentElement, nameSpaceID;        nsCOMPtr<nsIContent>	child;	nsresult		rv;	if (NS_FAILED(rv = container->ChildCount(numChildren)))	return(rv);	if (numChildren < 1)	return(NS_OK);	nsCOMPtr<nsIDocument> doc;	container->GetDocument(*getter_AddRefs(doc));	if (!doc)	return(NS_ERROR_UNEXPECTED);	// Note: This is a straight allocation (not a COMPtr) so we	// can't return out of this routine until/unless we free it!	contentSortInfo ** contentSortInfoArray = new contentSortInfo*[numChildren + 1];	if(!contentSortInfoArray)	return(NS_ERROR_OUT_OF_MEMORY);	// Note: walk backwards (and add nodes into the array backwards) because	// we also remove the nodes in this loop [via RemoveChildAt()] and if we	// were to do this in a forward-looking manner it would be harder	// (since we also skip over non XUL:treeitem nodes)	nsCOMPtr<nsIAtom> tag;	currentElement = numChildren;	for (childIndex=numChildren-1; childIndex >= 0; childIndex--)	{		if (NS_FAILED(rv = container->ChildAt(childIndex, *getter_AddRefs(child))))	continue;		if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID)))	continue;		if (nameSpaceID == kNameSpaceID_XUL)		{			if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag))))	continue;			if (tag.get() == kTreeItemAtom)			{				--currentElement;				nsCOMPtr<nsIRDFResource> resource;				gXULUtils->GetElementResource(child, getter_AddRefs(resource));				contentSortInfo *contentInfo = CreateContentSortInfo(child, resource);				if(contentInfo)					contentSortInfoArray[currentElement] = contentInfo;				++numElements;				// immediately remove the child node, and ignore any errors				container->RemoveChildAt(childIndex, PR_FALSE);			}		}	}	if (numElements > 0)	{		/* smart sorting (sort within separators) on name column */		if (sortInfo->inbetweenSeparatorSort == PR_TRUE)		{			PRInt32	startIndex=currentElement;			nsAutoString	type;			for (loop=currentElement; loop< currentElement + numElements; loop++)			{				if (NS_SUCCEEDED(rv = contentSortInfoArray[loop]->content->GetAttribute(kNameSpaceID_RDF,				    kRDF_type, type)) && (rv == NS_CONTENT_ATTR_HAS_VALUE))				{					if (type.EqualsWithConversion(kURINC_BookmarkSeparator))					{						if (loop > startIndex+1)						{                		    if (merelyInvertFlag == PR_TRUE)                		    {                		        InvertTreeChildren(&contentSortInfoArray[startIndex], loop-startIndex);                		    }                		    else                		    {    							NS_QuickSort((void *)&contentSortInfoArray[startIndex], loop-startIndex,    								sizeof(contentSortInfo *), testSortCallback, (void *)sortInfo);    						}							startIndex = loop+1;						}					}				}			}			if (loop > startIndex+1)			{    		    if (merelyInvertFlag == PR_TRUE)    		    {    		        InvertTreeChildren(&contentSortInfoArray[startIndex], loop-startIndex);    		    }    		    else    		    {    				NS_QuickSort((void *)&contentSortInfoArray[startIndex], loop-startIndex, sizeof(contentSortInfo *),    					testSortCallback, (void *)sortInfo);    			}				startIndex = loop+1;			}		}		else		{		    if (merelyInvertFlag == PR_TRUE)		    {		        InvertTreeChildren(&contentSortInfoArray[currentElement], numElements);		    }		    else		    {    			NS_QuickSort((void *)(&contentSortInfoArray[currentElement]), numElements,    			    sizeof(contentSortInfo *), testSortCallback, (void *)sortInfo);	    	}		}		nsCOMPtr<nsIContent>	parentNode;		nsAutoString		value;		PRInt32			childPos = 0;		// recurse on grandchildren		for (loop=currentElement; loop < currentElement + numElements; loop++)		{			nsIContent* kid = NS_STATIC_CAST(nsIContent*, contentSortInfoArray[loop]->content);			// Because InsertChildAt() only does a			// "shallow" SetDocument(), we need to do a			// "deep" one now...			kid->SetDocument(doc, PR_TRUE, PR_TRUE);			container->InsertChildAt(kid, childPos++, PR_FALSE);			parentNode = (nsIContent *)contentSortInfoArray[loop]->content;			contentSortInfo * contentSortInfo = contentSortInfoArray[loop];			NS_RELEASE(contentSortInfo->content);			delete contentSortInfo;			// if its a container, find its treechildren node, and sort those			if (NS_FAILED(rv = parentNode->GetAttribute(kNameSpaceID_None, kContainerAtom, value)) ||				(rv != NS_CONTENT_ATTR_HAS_VALUE) || (!value.EqualsIgnoreCase(*trueStr)))				continue;			if (NS_FAILED(rv = parentNode->ChildCount(numChildren)))	continue;			for (childIndex=0; childIndex<numChildren; childIndex++)			{				if (NS_FAILED(rv = parentNode->ChildAt(childIndex, *getter_AddRefs(child))))					continue;				if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID)))	continue;				if (nameSpaceID != kNameSpaceID_XUL)	continue;				if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag))))	continue;				if (tag.get() != kTreeChildrenAtom)	continue;				sortInfo->parentContainer = parentNode;				SortTreeChildren(child, sortInfo, merelyInvertFlag);			}		}	}	delete [] contentSortInfoArray;	contentSortInfoArray = nsnull;	return(NS_OK);}