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

sortby plugin why wont it sort numerically?

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
jaze22
Smarty Rookie


Joined: 06 Jan 2010
Posts: 6

PostPosted: Fri Dec 02, 2011 10:08 pm    Post subject: sortby plugin why wont it sort numerically? Reply with quote

This code will sort alphabetically ok for example via title or description. But when I try to get it to sort by views or rating which are numerical it does not work.

Any ideas why it will not sort numerically? I have been trying for hours now.

Code:
<?php
function array_sort_by_fields(&$data, $sortby){
    if(is_array($sortby)) {
        $sortby = join(',', $sortby);
    }
    uasort( $data,
         create_function( '$a, $b', '
            $skeys = split(\',\',\''.$sortby.'\');
            foreach($skeys as $key){
               if( ($c = strcasecmp($a[$key],$b[$key])) != 0 ){
                            return($c);
               }
            }
           return($c);  '));
}

#
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field
#
function smarty_modifier_sortby($arrData, $sortfields) {
   array_sort_by_fields($arrData, $sortfields);
   return $arrData;
}

$smarty->register_modifier( "sortby", "smarty_modifier_sortby" );   
?>



Usage in template
Code:
{foreach from=$recent_videos|@sortby:"views" item=file name=file}
<div>blah</div>
{/foreach}
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 -> Plugins 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