CSort

$sort=new CSort;
$sort->modelClass='Entity';
$sort->attributes=array(
  'asc' => 'title',
  'desc' => 'title DESC',
);
 
foreach(Entity::model()->attributeNames() as $attr)
    $sort->attributes[]="relatedEntity.$attr";
 
//Important! The default separator uses a dot for separate the direction from the attribute, and the related attribute is separated by a dot too!
$sort->separator=array(',','-');