Sorting Resources by Name

Sorting Resources

When working with a project using Aspose.Tasks, project resources can be sorted by any of the fields. This article shows how to implement the IComparable interface to sort resources by name. To accomplish this, first, define a class that implements the IComparable interface as shown below. Then traverse the resources in the project and sort them by name.

Implementing the IComparable Interface

 1int32_t SortResourcesByName::RscNameComparer::Compare(System::SharedPtr<Resource> const &x, System::SharedPtr<Resource> const &y) ASPOSE_CONST
 2{
 3    if (System::String::IsNullOrEmpty(x->Get<System::String>(Rsc::Name())))
 4    {
 5        return 1;
 6    }
 7    if (System::String::IsNullOrEmpty(y->Get<System::String>(Rsc::Name())))
 8    {
 9        return -1;
10    }
11    return x->Get<System::String>(Rsc::Name()).CompareTo(y->Get<System::String>(Rsc::Name()));
12}

Sorting Resources by Name

 1System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"project-sort.mpp");
 2    
 3System::SharedPtr<System::Collections::Generic::List<System::SharedPtr<Resource>>> resources = project->get_Resources()->ToList();
 4resources->Sort(System::MakeObject<SortResourcesByName::RscNameComparer>());
 5    
 6    
 7{
 8    auto rsc_enumerator = (resources)->GetEnumerator();
 9    decltype(rsc_enumerator->get_Current()) rsc;
10    while (rsc_enumerator->MoveNext() && (rsc = rsc_enumerator->get_Current(), true))
11    {
12        System::Console::WriteLine(rsc);
13    }
14}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.