Home - Forums-.NET - FlyGrid.Net (Windows Forms) - Sorting subnodes

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

Sorting subnodes
Link Posted: 14-Apr-2006 00:37
Hi,

I have a hierachy column and the nodes in it have subnodes. I'd like to sort the subnodes by a certain column when they are added. I tried:

accountNode.Items.Sort(ColumnCache[\"symbol\"], accountNode.Items.Count, StringComparer.InvariantCulture);


but I'm getting: \"Offset and length were out of bounds for the array or is greater thatn the number of elements from index to the end of the source collection.\"

I guess it has to do with the \"count\" parameter in the sort method? Not sure what I should pass there?
Link Posted: 14-Apr-2006 18:49
Do not forget that you're sorting node's instances, not a strings.
For sorting purposes you can use your own IComparer implementation:
NB: with the nearest update you can perform sorting with NineRays.Windows.Forms.Data.DefaultComparer that supports multiple columns sorting
[c#]
private void SortNodes(NodeBase node, Column col)
{
  NodesComparer comparer = new NodesComparer(col);
  node.Items.Sort(comparer);
}

public class NodesComparer : IComparer
{
  private Column col;
  public NodesComparer(Column col)
  {
    this.col = col;
  }
  #region IComparer Members
  public int Compare(object x, object y)
  {
    Node xnode = x as Node;
    Node ynode = y as Node;
    int result = 0 ;
    object xobj = xnode.GetCellValue(col);
    object yobj = ynode.GetCellValue(col);
    IComparable xvalue = xobj as IComparable;
    IComparable yvalue = yobj as IComparable;
    if (xvalue != null && col.SortOrder == SortOrder.Ascending)
    {
      result = xvalue.CompareTo(yvalue);
    }
    else if (yvalue != null && col.SortOrder == SortOrder.Descending)
    {
      result = yvalue.CompareTo(xvalue);
    }
    return result;
  }
  #endregion
}
Link Posted: 14-Apr-2006 23:46
Well, I thought the first parameter in the sort method specifies the column by which to sort and that is type of string. Anyways, thanks for the sample code
Link Posted: 19-Apr-2006 07:53
Here is more correct code (in previous example sorting performed on old SortOrder of column, but if we use sorting in ColumnSortOrderChanging we should use new SortOrder value):
[c#]
private void SortNodes(NodeBase node, Column col, SortOrder newOrder)
{
  NodesComparer comparer = new NodesComparer(col, newOrder);
  node.Items.Sort(comparer);
}

public class NodesComparer : IComparer
{
  private Column col;
  private SortOrder order;
  public NodesComparer(Column col, SortOrder order)
  {
    this.col = col;
    this.order = order;
  }
  #region IComparer Members
  public int Compare(object x, object y)
  {
    Node xnode = x as Node;
    Node ynode = y as Node;
    int result = 0 ;
    object xobj = xnode.GetCellValue(col);
    object yobj = ynode.GetCellValue(col);
    IComparable xvalue = xobj as IComparable;
    IComparable yvalue = yobj as IComparable;
    if (xvalue != null && order == SortOrder.Ascending)
    {
      result = xvalue.CompareTo(yvalue);
    }
    else if (yvalue != null && order == SortOrder.Descending)
    {
      result = yvalue.CompareTo(xvalue);
    }
    return result;
  }
  #endregion
}