Page 1 of 1

CGCalendar Sort Upcoming List SOLVED

Posted: Fri Jan 24, 2014 1:42 pm
by clj83
I have used this technique successfully many times before but in this case is it not working and I cannot see why? I am trying to sort the array in my upcoming list in the CGCalendar module. I am using the following UDT:

Code: Select all

if (!function_exists('do_sort')) {
    function do_sort($a, $b) {
        return $a->event_title > $b->event_title;
    }
}
$data = $params['data'];
usort($data, 'do_sort');
$smarty->assign('sorted', $data);
So this should sort the foreach loop so that the event titles are in order. It does not work though. Could it because there is something special about the array?

Code: Select all

Array ( 
[event_id] => 750 
[event_title] => Dirty Habit 
[event_summary] => 
[event_details] => 
[event_date_start] => 2014-02-22 20:00:00 
[event_date_end] => 
[event_parent_id] => 731
[event_recur_period] => 
[event_date_recur_end] => 
[event_created_by] => -106 
[event_create_date] => 2014-01-24 10:49:00 [event_modified_date] => 2014-01-24 10:49:00 [event_recur_nevents] => [event_recur_interval] => [event_recur_weekdays] => [event_recur_monthdays] => [event_allows_overlap] => 1 
) 
Anyone have any ideas?

Re: CGCalendar Sort Upcoming List

Posted: Fri Jan 24, 2014 5:18 pm
by JohnnyB
return $a->event_title > $b->event_title;
have you tried
return $a['event_title'] > $b['event_title']

Re: CGCalendar Sort Upcoming List

Posted: Fri Jan 24, 2014 9:27 pm
by velden
Think JohnnyB is right. You're probably trying to sort an array of arrays and not an array of objects.

Further, consider using:

Code: Select all

return strcasecmp($a->event_title,$b->event_title);
It will sort case insensitive and maybe give more expected results.

Re: CGCalendar Sort Upcoming List

Posted: Sat Jan 25, 2014 8:13 am
by Rolf

Re: CGCalendar Sort Upcoming List

Posted: Sat Jan 25, 2014 10:37 am
by velden
Rolf wrote:Are you looking for this?
https://www.cmscanbesimple.org/blog/sort-array-modifier
I don't think so as that will 'only' sort an single dimensional array* while the OP has a multi dimensional array (array in an array) and wants to sort on a specific value (value of event_title).

* maybe it will sort a multi dimensional array but probably the result won't be what you'd expect.

Re: CGCalendar Sort Upcoming List

Posted: Sat Jan 25, 2014 1:49 pm
by clj83
JohnnyB's solution worked.

Code: Select all

if (!function_exists('do_sort')) {
    function do_sort($a, $b) {
        return $a['event_title'] > $b['event_title'];
    }
}
$data = $params['data'];
usort($data, 'do_sort');
$smarty->assign('sorted', $data);
Thanks everyone for your help.

Re: CGCalendar Sort Upcoming List SOLVED

Posted: Sat Jan 25, 2014 4:49 pm
by velden
I should have typed:

Code: Select all

if (!function_exists('do_sort')) {
    function do_sort($a, $b) {
        return strcasecmp($a['event_title'],$b['event_title']);
    }
}
$data = $params['data'];
usort($data, 'do_sort');
$smarty->assign('sorted', $data);