Sorting Tasks by Name

Contents
[ Hide Show ]

Sorting Tasks by Name

While working with a project using Aspose.Tasks, the tasks in the project can be sorted by any of the fields. This article shows how to implement the IComparable interface to sort tasks by name.

First, define a class that implements the IComparable interface. Then traverse the tasks in the project and sort them by name.

 1private class TaskNameComparer : IComparer<Task>
 2{
 3    public int Compare(Task x, Task y)
 4    {
 5        if (string.IsNullOrEmpty(x.Get(Tsk.Name)))
 6            return 1;
 7        if (string.IsNullOrEmpty(y.Get(Tsk.Name)))
 8            return -1;
 9        return x.Get(Tsk.Name).CompareTo(y.Get(Tsk.Name));
10    }
11}
 1Project project = new Project("New Project.mpp");
 2ChildTasksCollector coll = new ChildTasksCollector();
 3TaskUtils.Apply(project.RootTask, coll, 0);
 4List<Task> tasks = coll.Tasks;
 5
 6tasks.Sort(new TaskNameComparer());
 7
 8foreach (Task task in tasks)
 9{
10    Console.WriteLine(task);
11}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.