CGCalendar Sort Upcoming List SOLVED

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
clj83
Forum Members
Forum Members
Posts: 195
Joined: Tue Jul 07, 2009 4:09 pm

CGCalendar Sort Upcoming List SOLVED

Post 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?
Last edited by clj83 on Sat Jan 25, 2014 1:51 pm, edited 1 time in total.
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: CGCalendar Sort Upcoming List

Post by JohnnyB »

return $a->event_title > $b->event_title;
have you tried
return $a['event_title'] > $b['event_title']
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: CGCalendar Sort Upcoming List

Post 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.
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: CGCalendar Sort Upcoming List

Post by Rolf »

- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: CGCalendar Sort Upcoming List

Post 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.
clj83
Forum Members
Forum Members
Posts: 195
Joined: Tue Jul 07, 2009 4:09 pm

Re: CGCalendar Sort Upcoming List

Post 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.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: CGCalendar Sort Upcoming List SOLVED

Post 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);
Post Reply

Return to “Modules/Add-Ons”