Hi,
I’m trying something quite special... here’s the context :
- I have a list of resources with pagetitle field build on this model : surname NAME
- I want getresources to sort by alphabetical order those resources, but considering the NAME
I tried several things, here’s the last which seems the most promising, but does not work.... :
- in getResources, I add a parameter "sortCollectionByNAME", and modify the code around line 265 :
$collection = $modx->getCollection('modResource', $criteria);
if ($sortCollectionByNAME) {
uksort($collection, "sortCollectionByNAME");
}
- in getResources, I add an include at the first lines :
@include_once 'assets/templates/mlab/snippet/sortFunctions.php';
- I wrote a special sort function to achieve this goal
function sortCollectionByNAME($a, $b) {
if ($a == $b) {
return 0;
}
// Recherche des mots en majuscules (modèles prenom NOM) - accepte les noms composés (avec '-' et ' ')
preg_match('~\b\h([[:upper:]](\-|\h)*)+\b~', $a->get('pagetitle'), $A);
preg_match('~\b\h([[:upper:]](\-|\h)*)+\b~', $b->get('pagetitle'), $B);
return strcmp($A, $B);
}
Regarding this reference page on PHP sorting (
http://fr2.php.net/manual/en/function.usort.php), the third examples explain how to make a sort function for an object. It implies to write a static function for the object class and so plunge into the code....
Is there a way to achieve that in this direction or am I completly wrong?
In fact, I found a simple solution by adding to each resource a TV containing only the NAME and make this getResource call
[[!getResources? &parents=`[[*id]]` &limit=`0` &where=`{"template:=":6}` &sortbyTV=`NameTV` &sortdirTV=`ASC` &includeTVs=`1` &tpl=`ListeMembresTpl`]]
but is it possible to do it without this double entry (pagetitle + NameTV)? Any idea?
Thanks!