Customize sorting of Treeview

elfv picture elfv · Jun 21, 2011 · Viewed 9.5k times · Source

I have a treeview that need to be sorted according to the tag of every node and also according to the alpha beta.

for example:

  • Node1 , tag=A , text= Apple
  • Node2, tag=B , text= Baloon
  • Node3, tag=A, text= Help

I want to sort it, that nodes with tag A will be firsts, and just then nodes with tag B. but, i want the nodes that contains tag A, to be sorted from A to Z.

(the order = Node1,Node3,Node2)

please help me , How can i do it?

thanks in advance!

Answer

Chris Trombley picture Chris Trombley · Jun 21, 2011

Assuming you're talking about System.Windows.Forms.Treeview, you can use TreeViewNodeSorter and an implementation of IComparer to create a custom sorting strategy.

http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.treeviewnodesorter.aspx