Sort Tasks by Column in Gantt Chart

Aspose.Tasks for .NET provides the ability to sort tasks by any column in the Gantt chart view. This is accomplished with the help of the comparer method SaveOptions.TasksComparer before rendering in Gantt chart. The default comparer sorts tasks by task ID if no other option is specified.

 1class SortTasksByColumnInGanttChart
 2{
 3    public static void Run()
 4    {
 5        Project project = new Project("New Project.mpp");
 6        SaveOptions options = new PdfSaveOptions();
 7        options.Timescale = Timescale.Months;
 8
 9        options.TasksComparer = new TasksNameComparer();
10        project.Save("SortedByNames_out.pdf", options);
11
12        options.TasksComparer = new TasksDurationComparer();
13        project.Save("SortedByDurations_out.pdf", options);
14    }
15
16    public class TasksNameComparer : IComparer<Task>
17    {
18        public int Compare(Task x, Task y)
19        {
20            return x.Get(Tsk.Name).CompareTo(y.Get(Tsk.Name));
21        }
22    }
23
24    public class TasksDurationComparer : IComparer<Task>
25    {
26        public int Compare(Task x, Task y)
27        {
28            Duration durX = x.Get(Tsk.Duration);
29            Duration durY = y.Get(Tsk.Duration);
30            return durX.TimeSpan.CompareTo(durY.TimeSpan);
31        }
32    }
33}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.