title: Sorting child nodes of a treeview after populating the treeview in c# winforms
body: <p>I am having trouble sorting child nodes of a treeview in my winforms program. My treeview is populated by some XML files and it uses an internal text inside the xml files as Text property of nodes (So I think I cant sort them before adding them to the tree view, or if it is possible, since the xml files are big in size I dont want to waste the process). A populated treeview in my program looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/AciuV.png" alt="enter image description here"></p>
  
  <p>As you can guess I want child nodes to sort like (I dont want HBM\D10 to come after HBM\D1) rather I want:</p>
  
  <pre><code>    HBM\D1
      HBM\D2
      HBM\D3
  etc...
  </code></pre>
  
  <p>I have already tried treeView1.Sort() and also adding beginUpdate and endUpdate but I had no suceess :(</p>
  
  <p>I am using .NET 4, any tips would be appriciated</p>
  
  <p><strong>ok I sortet it out using Thomas's advice:</strong></p>
  
  <pre><code>    class NodeSorter : IComparer
  {
          public int Compare(object x, object y) 
          {         
              TreeNode tx = (TreeNode)x; 
              TreeNode ty = (TreeNode)y;
  
              if (tx.Text.Length &lt; ty.Text.Length)
              {
                  return -1;
              }
  
              if (tx.Text.Length &gt; ty.Text.Length)
              {
                  return 1;
              }
  
              return 0;
          } 
  }
  </code></pre>