Sorting Resources by Name
Sorting Resources by Name
Aspose.Tasks for .NET allows sorting project resources based on various fields, including their names. This guide demonstrates how to sort resources by name using a custom comparer.
Implementing a Custom Comparer
To sort resources by name, create a class that implements the IComparer<Resource>
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.