sortInfo.kNaturalOrderPosAtom = kNaturalOrderPosAtom;
XULSortServiceImpl::InsertContainerNode(nsIRDFCompositeDataSource *db, nsIContent *root,					nsIContent *container, nsIContent *node, PRBool aNotify){	nsresult	rv;	nsAutoString	sortResource, sortDirection, sortResource2;	_sortStruct	sortInfo;	// get composite db for tree	sortInfo.rdfService = gRDFService;	sortInfo.db = db;	sortInfo.resCache = nsnull;	sortInfo.mInner = nsnull;	sortInfo.colIndex = -1;	sortInfo.kNaturalOrderPosAtom = kNaturalOrderPosAtom;	sortInfo.kTreeCellAtom = kTreeCellAtom;	sortInfo.kNameSpaceID_XUL = kNameSpaceID_XUL;	sortInfo.sortProperty = nsnull;	sortInfo.sortProperty2 = nsnull;	PRBool			sortInfoAvailable = PR_FALSE;	if (IsTreeElement(root) == PR_TRUE)	{		// tree, so look for treecol node(s) which provide sorting info		if (NS_SUCCEEDED(rv = GetSortColumnInfo(root, sortResource, sortDirection, sortResource2)))		{			sortInfoAvailable = PR_TRUE;		}	}	else	{		// not a tree, so look for sorting info on root node		if (NS_SUCCEEDED(rv = root->GetAttribute(kNameSpaceID_None, kSortResourceAtom,			sortResource)) && (rv == NS_CONTENT_ATTR_HAS_VALUE))		{			if (NS_SUCCEEDED(rv = root->GetAttribute(kNameSpaceID_None, kSortDirectionAtom,				sortDirection)) && (rv == NS_CONTENT_ATTR_HAS_VALUE))			{				sortInfoAvailable = PR_TRUE;				if (NS_FAILED(rv = root->GetAttribute(kNameSpaceID_None, kSortResource2Atom,					sortResource2)) || (rv != NS_CONTENT_ATTR_HAS_VALUE))				{					sortResource2.Truncate();				}			}		}	}	if (sortInfoAvailable != PR_TRUE)		return(NS_ERROR_UNEXPECTED);	rv = gRDFService->GetResource(nsCAutoString(sortResource), getter_AddRefs(sortInfo.sortProperty));	if (NS_FAILED(rv))	return(rv);	if (sortResource2.Length() > 0)	{		rv = gRDFService->GetResource(nsCAutoString(sortResource2), getter_AddRefs(sortInfo.sortProperty2));		if (NS_FAILED(rv))	return(rv);	}	// set up sort order info	sortInfo.naturalOrderSort = PR_FALSE;	sortInfo.descendingSort = PR_FALSE;	if (sortDirection.EqualsIgnoreCase("descending"))	{		sortInfo.descendingSort = PR_TRUE;	}	else if (!sortDirection.EqualsIgnoreCase("ascending"))	{		sortInfo.naturalOrderSort = PR_TRUE;	}	PRBool			isContainerRDFSeq = PR_FALSE;	if ((sortInfo.db) && (sortInfo.naturalOrderSort == PR_TRUE))	{		// walk up the content model to find the REAL		// parent container to determine if its a RDF_Seq		nsCOMPtr<nsIContent>		parent = do_QueryInterface(container, &rv);		nsCOMPtr<nsIContent>		aContent;		nsCOMPtr<nsIDocument> doc;		if (NS_SUCCEEDED(rv) && parent) {			rv = parent->GetDocument(*getter_AddRefs(doc));			if (! doc)				parent = nsnull;		}		while(NS_SUCCEEDED(rv) && parent)		{			nsAutoString	id;			if (NS_SUCCEEDED(rv = parent->GetAttribute(kNameSpaceID_None, kIdAtom, id))				&& (rv == NS_CONTENT_ATTR_HAS_VALUE))			{				nsCOMPtr<nsIRDFResource>	containerRes;				rv = gXULUtils->MakeElementResource(doc, id, getter_AddRefs(containerRes));				if (NS_SUCCEEDED(rv)) {					rv = sortInfo.db->HasAssertion(containerRes,								       kRDF_instanceOf,								       kRDF_Seq,								       PR_TRUE,								       &isContainerRDFSeq);				}				break;			}			aContent = do_QueryInterface(parent, &rv);			if (NS_SUCCEEDED(rv))				rv = aContent->GetParent(*getter_AddRefs(parent));		}	}	PRBool			childAdded = PR_FALSE;	if ((sortInfo.naturalOrderSort == PR_FALSE) ||		((sortInfo.naturalOrderSort == PR_TRUE) &&		(isContainerRDFSeq == PR_TRUE)))	{		// figure out where to insert the node when a sort order is being imposed		// using a smart binary comparison		PRInt32			numChildren = 0;		if (NS_FAILED(rv = container->ChildCount(numChildren)))	return(rv);		if (numChildren > 0)		{		        nsCOMPtr<nsIContent>	child;			PRInt32			last = -1, current, direction = 0, delta = numChildren;			while(PR_TRUE)			{				delta = delta / 2;				if (last == -1)				{					current = delta;				}				else if (direction > 0)				{					if (delta == 0)	delta = 1;					current = last + delta;				}				else				{					if (delta == 0)	delta = 1;					current = last - delta;				}				if (current != last)				{					container->ChildAt(current, *getter_AddRefs(child));					nsIContent	*theChild = child.get();					direction = inplaceSortCallback(&node, &theChild, &sortInfo);				}				if ( (direction == 0) ||					((current == last + 1) && (direction < 0)) ||					((current == last - 1) && (direction > 0)) ||					((current == 0) && (direction < 0)) ||					((current >= numChildren - 1) && (direction > 0)) )				{					if (current >= numChildren)					{						container->AppendChildTo(node, aNotify);					}					else					{						container->InsertChildAt(node,							((direction > 0) ? current + 1: (current >= 0) ? current : 0),							aNotify);					}					childAdded = PR_TRUE;					break;				}				last = current;			}		}	}	if (childAdded == PR_FALSE)	{		container->AppendChildTo(node, aNotify);	}	return(NS_OK);}