We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8930
    • 44 Posts
    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? huh



    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!


      Equilibre Webdesign - Conception et r
      • 22303 MODX Staff
      • 10,725 Posts
      You can’t post-sort the objects in getResources — you need to do it in the sortby property (i.e. via SQL expression). Why is &sortby=`pagetitle` not good enough for your purposes?
        • 8930
        • 44 Posts
        Thanks OpenGeek to take some time to consider my question.

        Well, in fact my pagetitle is always on this model "surname NAME". So if you sort by pagetitle, it will perform it on the surname... and I want it by NAME!

        It seems that there are 3 solutions :
        - introduce a NAMETV used for sorting. It repeats a part of the pagetitle entry but it works well
        - change the pagetitle model to "NAME surname". It will be ready for sorting but I would need to switch each time I want to display "surname NAME"...
        - use pagetitle for NAME and use a TV for surname....

        From a conceptual and performance point of view, which solution do you think is the best?

        Thanks in advance




          Equilibre Webdesign - Conception et r