If one does not already exist for it, sure...
That said, should I post a request for post-query sorting on your gitHub page?1
Hi Jason, if you’re reading this is there any way we could make sorting numerical TVs in getResources a little more user-friendly out of the box? I’m sure it will catch quite a lot of people out... and the solutions above are not exactly straightforward. I’m still trying to work out a simple way to do it.
if (!empty($sortbyTV)) {
$criteria->leftJoin('modTemplateVar', 'tvDefault', array(
"tvDefault.name" => $sortbyTV
));
$criteria->leftJoin('modTemplateVarResource', 'tvSort', array(
"`tvSort`.`contentid` = `modResource`.`id`",
"`tvSort`.`tmplvarid` = `tvDefault`.`id`"
));
$criteria->select("IFNULL(`tvSort`.`value`, `tvDefault`.`default_text`) AS `sortTV`");
$criteria->sortby("`sortTV`", $sortdirTV);
}if (!empty($sortbyTV)) {
$criteria->leftJoin('modTemplateVar', 'tvDefault', array(
"tvDefault.name" => $sortbyTV
));
$criteria->leftJoin('modTemplateVarResource', 'tvSort', array(
"`tvSort`.`contentid` = `modResource`.`id`",
"`tvSort`.`tmplvarid` = `tvDefault`.`id`"
));
$criteria->select("IFNULL(`tvSort`.`value`, `tvDefault`.`default_text`) AS `sortTV`");
if (!empty($tvNumericSort)){
$criteria->sortby("CAST(`sortTV` AS DECIMAL)", $sortdirTV);
} else {
$criteria->sortby("`sortTV`", $sortdirTV);
}
}[[!getResources? &parents=`4,10,5` &tpl=`myTpl` &includeTVs=`1` &sortbyTV=`myTvVar` &sortdirTV=`DESC` &tvNumericSort=`1` ]]
Quote from: lucas at Mar 09, 2011, 12:33 PM
Hi Jason, if you’re reading this is there any way we could make sorting numerical TVs in getResources a little more user-friendly out of the box? I’m sure it will catch quite a lot of people out... and the solutions above are not exactly straightforward. I’m still trying to work out a simple way to do it.
Hi!
I needed it too. Just as Jason said TV value is always string. After little googling I find simple solution. Ofcourse there is small change of the getResources code, and one more conditional parameter (tvNumericSort for forcing sort numeric).
Original code from line 195..
if (!empty($sortbyTV)) { $criteria->leftJoin('modTemplateVar', 'tvDefault', array( "tvDefault.name" => $sortbyTV )); $criteria->leftJoin('modTemplateVarResource', 'tvSort', array( "`tvSort`.`contentid` = `modResource`.`id`", "`tvSort`.`tmplvarid` = `tvDefault`.`id`" )); $criteria->select("IFNULL(`tvSort`.`value`, `tvDefault`.`default_text`) AS `sortTV`"); $criteria->sortby("`sortTV`", $sortdirTV); }
Change to:
if (!empty($sortbyTV)) { $criteria->leftJoin('modTemplateVar', 'tvDefault', array( "tvDefault.name" => $sortbyTV )); $criteria->leftJoin('modTemplateVarResource', 'tvSort', array( "`tvSort`.`contentid` = `modResource`.`id`", "`tvSort`.`tmplvarid` = `tvDefault`.`id`" )); $criteria->select("IFNULL(`tvSort`.`value`, `tvDefault`.`default_text`) AS `sortTV`"); if (!empty($tvNumericSort)){ $criteria->sortby("CAST(`sortTV` AS DECIMAL)", $sortdirTV); } else { $criteria->sortby("`sortTV`", $sortdirTV); } }
And call getResources like this:
[[!getResources? &parents=`4,10,5` &tpl=`myTpl` &includeTVs=`1` &sortbyTV=`myTvVar` &sortdirTV=`DESC` &tvNumericSort=`1` ]]
I hope this will help someone.
Yes my mistake
Quote from: dj13 at Mar 19, 2011, 01:19 PM
Quote from: lucas at Mar 09, 2011, 12:33 PM
Hi Jason, if you’re reading this is there any way we could make sorting numerical TVs in getResources a little more user-friendly out of the box? I’m sure it will catch quite a lot of people out... and the solutions above are not exactly straightforward. I’m still trying to work out a simple way to do it.
Hi!
I needed it too. Just as Jason said TV value is always string. After little googling I find simple solution. Ofcourse there is small change of the getResources code, and one more conditional parameter (tvNumericSort for forcing sort numeric).
Original code from line 195..
if (!empty($sortbyTV)) { $criteria->leftJoin('modTemplateVar', 'tvDefault', array( "tvDefault.name" => $sortbyTV )); $criteria->leftJoin('modTemplateVarResource', 'tvSort', array( "`tvSort`.`contentid` = `modResource`.`id`", "`tvSort`.`tmplvarid` = `tvDefault`.`id`" )); $criteria->select("IFNULL(`tvSort`.`value`, `tvDefault`.`default_text`) AS `sortTV`"); $criteria->sortby("`sortTV`", $sortdirTV); }
Change to:
if (!empty($sortbyTV)) { $criteria->leftJoin('modTemplateVar', 'tvDefault', array( "tvDefault.name" => $sortbyTV )); $criteria->leftJoin('modTemplateVarResource', 'tvSort', array( "`tvSort`.`contentid` = `modResource`.`id`", "`tvSort`.`tmplvarid` = `tvDefault`.`id`" )); $criteria->select("IFNULL(`tvSort`.`value`, `tvDefault`.`default_text`) AS `sortTV`"); if (!empty($tvNumericSort)){ $criteria->sortby("CAST(`sortTV` AS DECIMAL)", $sortdirTV); } else { $criteria->sortby("`sortTV`", $sortdirTV); } }
And call getResources like this:
[[!getResources? &parents=`4,10,5` &tpl=`myTpl` &includeTVs=`1` &sortbyTV=`myTvVar` &sortdirTV=`DESC` &tvNumericSort=`1` ]]
I hope this will help someone.
It doesn’t work with "." Example: 9.8 - 8.8 - 7.8 - 10

$criteria->sortby("CAST(`sortTV` AS DECIMAL)", $sortdirTV);$criteria->sortby("CAST(`sortTV` AS DECIMAL(10,2))", $sortdirTV);Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99.