The following small piece of code should serve as a good example for Ditto’s custom sort without altering the core code of the snippet.
The problem was originally formulated in this post on the MODx forum. Documents have template variable of type date attached, moreover, formatter widget was selected for this date TV thus enabling user for convenient insertion of a specific value. However, it significantly complicated using this TV as a sort criterion. Formatter widget transforms Unix timestamp (easy-to-use for sorting purposes) to human-readable textual format.
In order to sort documents by this „malformed date”, we need to convert it back to Unix timestamp:
Code of Ditto’s extender for custom sorting
<?php
if (!function_exists('sortByDatePicker')) {
function sortByDatePicker($a_doc, $b_doc) {
$a_stamp = strtotime($a_doc['date']);
$b_stamp = strtotime($b_doc['date']);
return ($a_stamp < $b_stamp ? -1 : ($a_stamp > $b_stamp ? 1 : 0));
}
}
$orderBy['custom'][] = array('date', 'sortByDatePicker');
$ditto->advSort = TRUE;
?>
date in the above code stands for the name of a user-inserted TV of type date. We need to put this code into, for example, sortByDatePicker.extender.inc.php file, move the file to assets/snippets/ditto/extenders folder, and call Ditto with newly-created extender.
Ditto call
[[Ditto? ... &extenders=`sortByDatePicker` ... ]]
- Required fields are marked with *.
- Comments are published after the approval of the site moderator, who is a human rather than a wind-fast computer program so please be patient. Off-topic comments are definitely removed.
| Enter this code into the appropriate field of the form while submitting an e-mail or a comment. If you have trouble with reading it, generate another... |


