GanttView for WPF | ComponentOne
In This Topic
    Sort
    In This Topic

    Sorting is an important requirement when it comes to data management as it helps users to view data in a specified order. The GanttView control lets you sort tasks by task name, start/finish date and duration through various sorting options.

    Lets discuss how you can apply sorting in GanttView.

    Apply Sort

    To apply sorting in GanttView, you can use SortTasks method of the C1GanttView class. The SortTasks method has two overloads which are discussed in the following table.

    Overload Description
    SortTasks(System.Collections.Generic.IComparer<C1.GanttView.Task> nextComparer) Sorts all the tasks by given comparer.
    SortTasks(System.ComponentModel.PropertyDescriptor prop, System.ComponentModel.ListSortDirection direction) Sorts all the tasks by specific property and order direction.

    Use the following code to apply sorting in GanttView. Here, GanttView tasks are sorted in an ascending order on the basis of their names.

    C#
    Copy Code
    var propertyDescriptors = TypeDescriptor.GetProperties(typeof(Task));
    gv.SortTasks(propertyDescriptors["Name"], ListSortDirection.Ascending);
    

    Alternatively, you can apply sorting using the Sort button on the Toolbar. The Sort button opens the Sort menu which provides various options described in the following table:

    Sort Option Description
    By Name Sorts tasks by their name in alphabetical order.
    By Start Date Sorts the tasks by their start date.
    By Finish Date Sorts the tasks by their finish date.
    By Duration Sorts the tasks by the time duration assigned to each task.
    Remove Sort Removes any existing sort condition.
    Sort By Enables multicolumn sorting.
    By default, all the sorting options available under the Sort button display data in 'ascending' order. However, you can change the default sort order to 'descending' by using Sort dialog. For more information, see Sort dialog.

    Clear Sort

    To clear sorting and return to the default view, you can use RemoveSort method of the C1GanttView class. The RemoveSort method clears all the sorting applied to the tasks as demonstrated in the following code.

    C#
    Copy Code
        
    gv.RemoveSort();
    

    Custom Sort

    GanttView provides various options for sorting the tasks in ascending or descending order as per your needs. However, there might be a scenario where you would want to apply your own custom sorting.

    To implement custom sorting in the GanttView control, follow these steps. The following example sorts the tasks according to the increasing length of task names on a button click event.

    1. Create a class CustomComparer that implements the IComparer<Task> interface to sort the tasks by comparing the lengths of their names.
      C#
      Copy Code
      public class CustomComparer : IComparer<Task>
          {
              public int Compare(Task x, Task y)
              {
                  if (x.Name.Length > y.Name.Length)
                  {
                      return 1;
                  }
                  else if (x.Name.Length < y.Name.Length)
                  {
                      return -1;
                  }
                  else
                  {
                      return 0;
                  }
              }
          }   
      
    2. Subscribe to the button click event and add the following code to Sort_BtnClick event handler. Here, we create an instance of the CustomComparer class and pass it as a parameter to SortTasks method to specify the custom criteria for sorting the GanttView tasks.
      C#
      Copy Code
          
      private void Sort_BtnClick(object sender, RoutedEventArgs routedEventArgs)
      {
          CustomComparer customComparer = new CustomComparer();
          gv.SortTasks(customComparer);
      }
      

    Multi-Column Sort

    GanttView allows you to achieve multicolumn sorting through the Sort By option available under the Sort button on the toolbar. To achieve multicolumn sorting, GanttView provides Sort dialog that lets you set the sort order in multiple columns at runtime. For more information, see Sort dialog.

    The following GIF shows how you can implement multi-column sorting in GanttView.

    multi-column sorting in ganttview