이름으로 작업을 정렬합니다

aspose.tasks for .NET을 사용하여 프로젝트 데이터로 작업 할 때는 종종 분석 또는 디스플레이를위한 특정 순서로 작업을 구성해야합니다. 한 가지 일반적인 접근법은 이름으로 알파벳순으로 작업을 정렬하는 것입니다. 이 안내서는 사용자 정의 비교를 구현하여이를 수행하는 방법을 설명합니다.

사용자 정의 비교를 구현합니다

작업을 이름으로 정렬하려면icomparer <asking>인터페이스를 구현하는 클래스를 정의하십시오. 이 비교는 null 또는 빈 이름을 확인한 다음 문자열 비교를 적용합니다.

 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}

This logic ensures tasks without names are placed at the end of the list.

Collecting and Sorting Tasks

Load a project and collect its tasks using ChildTasksCollector. Then apply the custom comparer to sort them:

 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}

The ChildTasksCollector helps flatten the task hierarchy into a list, making it easier to work with. After sorting, tasks will appear in alphabetical order based on their names.

Summary

Sorting tasks by name can simplify reporting, filtering, and UI rendering. With a simple custom comparer and the task collection utilities in Aspose.Tasks, this process is easy to integrate into .NET workflows.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.