I have an explorer like tree which is bound to a datasource. I want to be able to sort by something other than the text of the node. For example, I have a "NodeType" field and the value is either Folder or File. I want all Folders to be on top, and for a bonus, files sorted by FileName (which is the text of the node) ascending.
I have heard there is a SortComparer thing that I can do but do not have any examples for it. Can someone help out with this? Also, is it possible to tell the tree or the sort comparer to Sort by NodeType first and then by FileName?
Thanks.
Here is an example compare function that I use
}
public int Compare(object x, object y) {
UltraTreeNodeCell cellY = y as UltraTreeNodeCell;
UltraTreeNode nodeY = y as UltraTreeNode;
nodeX = cellX.Node;
nodeY = cellY.Node;
//
// If the ALI object is null, then it is a folder.
// We want folder to be sorted on the top and ALI objects to be sorted on the bottom.
return -1;
#endregion
Hi,
I don't have a sample of this, but it's really pretty easy.
What you do is define a class that implements the IComparer interface. This interface has only one method called Compare. So you implement the Compare method, then set the SortComparer on the tree column to an instance of your IComparer class.
The implementation of the Compare method will pass you two objects, x and y. In the case of the tree, these objects will be TreeNodes. So you can examine the two nodes and you return a value from Compare indicating which node is greater.
Let me know if you get stuck and I will try to point you in the right direction.