Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Built in sort function ?

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 67

PostPosted: Fri Dec 27, 2019 3:56 am    Post subject: Built in sort function ? Reply with quote

smarty-3.1.33

Hello. I am wondering if there is a later version of Smarty, and if so - Does it contain a BUILT IN sort function for arrays? (the last i saw there was no built-in function).

Ive got an array with

- TOP LEVEL,
-- SECOND LEVEL.

each element of TOP LEVEL contains 1 or more SECOND LEVEL items, which i want to sort within the template.

Is this possible in the latest version ?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1615

PostPosted: Fri Dec 27, 2019 7:49 pm    Post subject: Reply with quote

https://www.smarty.net/forums/viewtopic.php?p=94753#94753
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 67

PostPosted: Sat Dec 28, 2019 12:03 am    Post subject: Reply with quote

AnrDaemon wrote:
https://www.smarty.net/forums/viewtopic.php?p=94753#94753


Whats this got to do with sorting an array ?
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 265
Location: West Coast

PostPosted: Sat Dec 28, 2019 1:33 am    Post subject: Reply with quote

Scanning all the Smarty code for 'sort' produced two results that pertain to sorting the names of the assigned variables that will eventually display in the debug popup window.

So, I think there is no Smarty-developed sorting function.

If your array is something like:

$myArray: array(
array('a','b','c'),
array('b','c','d'),
...
)

That is, an array(s) of arrays, then maybe iterate through the first dimension, using a PHP function to sort each second dimension.

But I cannot find any BUILT-IN sorting function within the core Smarty package.
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 67

PostPosted: Sat Dec 28, 2019 2:18 am    Post subject: Reply with quote

Found it - Include this before you do FOREACH (in template)

{$tmp = ksort($list)}

$list is the array you are foreaching (is that a word)

Code:

{$tmp = ksort($list)}
{foreach from=$list key=k item=v}

.. do stuff..

{/foreach}



EDIT - i dont use $tmp in any way - except to sort $list (or whatever variable)
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1615

PostPosted: Sat Dec 28, 2019 12:41 pm    Post subject: Reply with quote

sarahfoxnz wrote:
AnrDaemon wrote:
https://www.smarty.net/forums/viewtopic.php?p=94753#94753


Whats this got to do with sorting an array ?

http://xyproblem.info/

If you really want to sort data, do it in your code, Smarty shouldn't care about such things.
If you want to show menu in a specific order, use specific code that displays your menu.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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