We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6991
    • 15 Posts
    I would like to sort the blog entries by customized php date function other than unix date time stamp, or default gregorian DTS that modx use by default.
    I have seen this: http://ditto.modxcms.com/tutorials/sort-by-a-date-tv.html but is it about the way of including some customized date function to work with ditto?

    Thanks smiley
      • 18397
      • 3,250 Posts
      Ditto 2.1b3 has support for custom sort functions but there are no tutorials yet.
      To create one, you would need to create a new extender file with at least the following:
      $orderBy["custom"][] = array("sortBy","callback_function");
      

      In this example, sortBy is the field name to be passed to callback_function to sort.

      The function in the Ditto class uses usort when a custom orderBy is provided. Thus, your function will have to apply to the usort functions conventions.
        • 6991
        • 15 Posts
        Thanks Mark. ill download the beta rev you’v suggested.

        Correct me if i am wrong, orderBy is assigning a new source of date function? if so, is such function should be included in the ditto class itself or it might fetch some output from another snippet function to the class and then accomplishes the sorting task?

        i have found callback_function in ditto class but am not sure what exactly is trying to do with filters? or how i would define my function to it.

        Thanks smiley
          • 18397
          • 3,250 Posts
          A custom orderBy function replaces the default multisort function. Thus, the function should be contained within an extender. It has access to the resource array for that document including the sortBy field(s).

          Here is an example custom sort (to be placed in an extender):
          <?php
          $orderBy["custom"][] = array("weight","callme");
          if(!function_exists('callme')) {
          	function callme($a, $b)
          	{
          	    return ($a['weight'] > $b['weight']) ? -1 : 1;
          	}
          }
          ?>
          


          This function will sort by the field weight in descending order resulting in:

          # Just a pretend, older post - (Weight: 3)
          # Søme UTF-8 yä mån! - (Weight: 2)
          # YAP & YOU! - (Weight: 0)
          # Ooooh, another post... - (Weight: 0)
          # Mini-Blog HOWTO - (Weight: -1)