タスクを名前で並べ替えます
.NETのAsopse.tasksを使用してプロジェクトデータを操作する場合、分析または表示のために特定の順序でタスクを整理する必要があることがよくあります。一般的なアプローチの1つは、タスクを名前でアルファベット順に並べ替えることです。このガイドは、カスタム比較を実装することでそれを行う方法を説明します。
カスタム比較の実装
タスクを名前で並べ替えるには、「icomparer
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.
概要
タスクを名前順にソートすることで、レポート作成、フィルタリング、UI 表示を簡素化できます。Aspose.Tasks のタスクコレクション機能とシンプルなカスタム比較ロジックを使えば、このプロセスを .NET ワークフローに簡単に統合できます。