Modeling Services > Relations > List-relations > Sorted List-relations
 
Sorted List-relations
Sorted list-relations differ from simple list-relations in that they can be associated with a sort criterion that will be used to automatically determine where a new object will be placed in a list.
Rogue Wave® Server supplies two class templates to define sorted list-relations:
*IlsSortedOwnsList<OwnerType,OwnedType,Comparator>
*IlsSortedUsesList<UserType,UsedType,Comparator>
These class templates respectively derive from IlsOwnsList and IlsUsesList and both take the following three arguments:
*The owner or user type. This class must directly or transitively derive from IlsObject or IlsEntity. In addition, derivation must be public.
*The owned or used type. This class must directly or transitively derive from IlsObject as far as IlsSortedOwnsList is concerned and from IlsEntity or IlsObject as far as IlsSortedUsesList is concerned. In addition, derivation must be public.
*A Comparator class that defines a sort criterion.
Defining and Building Sorted List-relations
The procedure to define and build a sorted list-relation is identical to that used to define and construct a simple list-relation. Simple list-relations are discussed in section “Building List-relations”..
The Comparator Class
The class Comparator must contain one public static function with the following signature:
static IlsBoolean Compare(TargetType&, TargetType&);
This function compares two instances of the target type and returns IlsTrue if the first argument is greater than the second one. The list will then be ordered according to the sorting criterion specified by the Compare function.
Let us consider a route that owns flights. If we define the relation that binds objects of type Route to objects of type Flight using the class template IlsOwnsList, we obtain the following:
class Flight: public IlsObject
{
  public:
    Flight(IlsIdentifier identifier): _identifier(identifier){}
    IlsIdentifier getIdentifier() const {return _identifier;}
  private:
    IlsIdentifier _identifier;
};
 
class Route: public IlsObject
{
  public:
    Route(): _flights(*this){}
  private:
    IlsOwnsList<Route,Flight> _flights;
};
Let us suppose that we want to sort the flights by alphabetical order. Since each flight is associated with an alphanumeric identifier, we can use the class template IlsSortedOwnsList as follows:
class Flight: public IlsObject
{
  public:
    Flight(IlsIdentifier identifier): _identifier(identifier){}
  private:
    IlsIdentifier _identifier;
};
 
 
 
class Route: public IlsObject
{
  public:
    Route(): _flights(*this){}
  private:
  class Comparator{
    public:
      // Must return IlsTrue if the first argument
      // is greater than the second
      static IlsBoolean Compare(Flight& lhs, Flight& rhs){
          return strcmp(lhs.getIdentifier()->getValue(),
                        rhs.getIdentifier()->getValue())>0;
      }
  };
  IlsSortedOwnsList<Route,Flight,Comparator> _flights;
};
The list-relation _flight will be sorted according to the sort criterion defined by the compare function. In the above example, the list is sorted in ascending alphabetical order. To sort the same list in descending alphabetical order, you just have to redefine the Compare function as indicated in the code sample below where the function evaluates whether the first argument precedes the second argument in the alphabetical order:
class Comparator
{
  public:
   static IlsBoolean Compare(Flight& lhs, Flight& rhs){
     return strcmp(lhs.getIdentifier()->getValue(),
                   rhs.getIdentifier()->getValue())<0;
   }
};
The use of sorted lists should be limited to short lists. Lists that contains a very large number of objects and that are not likely to be extended—or only by a very few number of items—should be treated as simple lists and objects must be ordered by means of an appropriate sort algorithm, such as quick sort, before they are placed in the list.

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.