名前でリソースを並べ替えます

名前でリソースを並べ替える

.NETのAspose.tasksは、名前を含むさまざまなフィールドに基づいてプロジェクトリソースを並べ替えることができます。このガイドは、カスタム比較を使用して名前でリソースを並べ替える方法を示しています。

カスタム比較の実装

名前でリソースを並べ替えるには、 icomparer <sorstice> interfaceを実装するクラスを作成します。

 1private class RscNameComparer : IComparer<Resource>
 2{
 3    public int Compare(Resource x, Resource y)
 4    {
 5        if (string.IsNullOrEmpty(x.Get(Rsc.Name)))
 6        {
 7            return 1;
 8        }
 9
10        if (string.IsNullOrEmpty(y.Get(Rsc.Name)))
11        {
12            return -1;
13        }
14
15        return x.Get(Rsc.Name).CompareTo(y.Get(Rsc.Name));
16    }
17}

Loading and Sorting Resources

Once the comparer is defined, you can load a project and sort its resources:

1Project project = new Project("New Project.mpp");
2
3List<Resource> resources = project.Resources.ToList();
4resources.Sort(new RscNameComparer());
5
6foreach (Resource resource in resources)
7{
8    Console.WriteLine(resource);
9}

This approach ensures that resources are listed in alphabetical order by their names, while handling cases where the name field is missing or empty.