We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44922
    • 131 Posts
    We're using Jakoweb / AlanPich Modx Extra Glossary. It generates a nice front end page with A, B, C ... navigation which then link to a bunch of Glossary terms and explanations beginning with that letter. Only thing is, under each letter the terms are themselves not in alphabetical order, e.g:
    C
    Crocodile
    Cat
    Camel
    Crayfish

    We want
    C
    Camel
    Cat
    Crayfish
    Crocodile

    The terms per letter are all output to an [[+items]] placeholder. This seems to be set using the content of a $termsHTML variable:
    [// Output all terms (grouped)
    $groupsHTML = '';
    foreach ($letters as $letter => $terms) {
        $termsHTML = '';
        if (count($terms)) {
                 // Prepare Terms HTML
            foreach ($terms as $term) {
                $params = array_merge($term, array(
                    'anchor' => strtolower(str_replace(' ', '-', $term['term'])),
                    'letter' => $letter
                ));
                $termsHTML .= $modx->getChunk($termTpl, $params);
            };
            // Prepare letter wrapper HTML
            $groupsHTML .= $modx->getChunk($groupTpl, array(
                'items' => $termsHTML,
                'letter' => $letter
            ));
        };
    };
    


    The actual page within the manager IS output alphabetically. Would anyone have any idea how to sort the $termsHTML? Tried KSort but that didn't work. [ed. note: lancipoos last edited this post 5 years, 6 months ago.]
      • 3749
      • 24,544 Posts
      What does the each $terms array look like? Can you show at least the beginnings of one:

      return "<pre>" . print_r($terms, true) . "</pre>";
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 44922
        • 131 Posts
        Sure
        <pre>Array
        (
            [0] => Array
                (
                    [id] => 39
                    [term] => Term Title
                    [explanation] => An internationally agreed description of what this term is.
                    [modified] => 2018-07-18 11:40:36
                    [modified_by] => 0
                )
        
        )
        </pre>
          • 3749
          • 24,544 Posts
          Can you modify your code so the array looks like this:


          Array
          (
              [term] => Array
                  (
                      [id] => 39
                      [term] => Term Title
                      [explanation] => An internationally agreed description of what this term is.
                      [modified] => 2018-07-18 11:40:36
                      [modified_by] => 0
                  )
           
          )


          Then ksort() should work on it. If you can't modify the array, you can write your own compare function that looks at $key['term'] and uses one of the "user-defined" sorting functions here: http://php.net/manual/en/array.sorting.php.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting