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 mixed values

 
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
bandzo
Smarty n00b


Joined: 13 Feb 2015
Posts: 1

PostPosted: Fri Feb 13, 2015 10:21 am    Post subject: sortby mixed values Reply with quote

Hi
im use this function to sorting:
Code:
<?php

function array_sort_by_fields(&$data, $sortby){
    static $sort_funcs = array();

    if (empty($sort_funcs[$sortby]))
    {
        $code = "\$c=0;";
        foreach (explode(',', $sortby) as $key)
        {
            $d = '1';
            if (substr($key, 0, 1) == '-')
            {
                $d = '-1';
                $key = substr($key, 1);
            }
            if (substr($key, 0, 1) == '#')
            {
                $key = substr($key, 1);
                $code .= "if ( ( \$c = (\$a['$key'] - \$b['$key'])) != 0 ) return $d * \$c;\n";
            }
            else
            {
                #$code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n";
                $code .= "if ( \$a['$key'] > \$b['$key']) return $d * 1;\n";
                $code .= "if ( \$a['$key'] < \$b['$key']) return $d * -1;\n";
            }
        }
        $code .= 'return $c;';
        $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
    }
    else
    {
        $sort_func = $sort_funcs[$sortby];
    }
    uasort($data, $sort_func);
}

#
# 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;
}

?>


but there is a problem with mixed values for ex i have:
5m
10m
15m
50m
100m
I return after sorting:
100m
10m
15m
55m
5m
how to fix this?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Feb 14, 2015 6:00 am    Post subject: Reply with quote

Write your function properly, so it return desired results.
This is not a Smarty question, btw.
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