Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Sorting Array

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Wed Apr 02, 2014 8:47 pm    Post subject: Sorting Array Reply with quote

I am creating an array within a template using:

Code:
{foreach from=$items item=node}
{if $node->fielddefs.location01.value != ''}{$locations[] = $node->fielddefs.location01.value}{/if}
{if $node->fielddefs.location02.value != ''}{$locations[] = $node->fielddefs.location02.value}{/if}
{if $node->fielddefs.location02.value != ''}{$locations[] = $node->fielddefs.location03.value}{/if}
{/foreach}


I need to sort the array alphabetically. I have tried the following usort function:

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


It does now work because my array is not correct I guess? My array looks like this:

Code:
Array
(
    [0] => Any
    [1] => Chamonix
    [2] => Courmayeur
    [3] => Verbier
    [4] => Zermatt
    [5] => Verbier
    [6] => Chamonix
)


Can anyone help me achieve a sorted output?

Thanks
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Apr 02, 2014 9:18 pm    Post subject: Reply with quote

See http://www.php.net/manual/en/function.usort.php example 1

Your function do_sort must return 0, -1 or 1.
Your function does return true or false

Why don't you use sort?
See http://www.php.net/manual/en/function.sort.php
Back to top
View user's profile Send private message
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Wed Apr 02, 2014 9:25 pm    Post subject: Reply with quote

Thanks for your help. I would use sort if I could figure it out, I am a php newbie. I don't suppose you could help me create the sort function?

Thanks
Back to top
View user's profile Send private message
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Wed Apr 02, 2014 9:27 pm    Post subject: Reply with quote

Actually, think I have it. Does this look right?

Code:
$data = $params['data'];
sort($data);
foreach ($data as $key => $val) {
}
$smarty->assign('sorted', $data);
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Apr 02, 2014 9:40 pm    Post subject: Reply with quote

you don't need
Code:
foreach ($data as $key => $val) {
}
as it does nothing.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP