Good day, experts, again!

Again there was a problem with the work of the bitrix. I have set up a product search with sphinx. The task now is to set up sorting of the found products by user fields. It would seem not difficult. But if I understood the documentation correctly, then the search results can be sorted only by the rank and date fields. I tried to go the same way as for CIBlockElement, but in vain. Maybe someone faced with a similar? And I also have to filter after that by the search results. If someone tells me about this, I will be grateful!

    2 answers 2

    If anyone is interested, then I found a way around this bitrix slip. But at once I will make a reservation that he also has a serious disadvantage - if you use pagination on the site, then with this method filtering and sorting will only work on the page you are on.

    Save us result_modifier and PHP function - usort. First, create a class in init.php:

    class CCabinet_SortObject { function __cmp_ValueOf($a, $b, $name, $order) { if(is_set($a[$name]) && is_set($b[$name])) { if($order == 'ASC') return ($a[$name]<$b[$name])?true:false; elseif($order == 'DESC')return ($b[$name]>$a[$name])?false:true; } } function cmp_袙袗楔_袩袗袪袦袝孝袪_袙袗楔_袩袨袪携袛袨袣($a, $b) { return CCabinet_SortObject::__cmp_ValueOf($a, $b, "袩袗袪袗袦袝孝袪_小袨袪孝袠袪袨袙袣袠", "袩袨袪携袛袨袣_小袨袪孝袠袪袨袙袣袠"); } function cmp_袙袗楔_袩袗袪袦袝孝袪_袙袗楔_袩袨袪携袛袨袣($a, $b) { return CCabinet_SortObject::__cmp_ValueOf($a, $b, "袩袗袪袗袦袝孝袪_小袨袪孝袠袪袨袙袣袠", "袩袨袪携袛袨袣_小袨袪孝袠袪袨袙袣袠"); } 

    It will allow us to sort the finished array after the search. We will call it in result_modifier in this way:

     if(isset($_GET["by"]) && isset($_GET["order"])) { usort($arResult['袦袗小小袠袙'],array("CCabinet_SortObject","cmp_".$_GET["by"]."_".$_GET["order"])); } 

    That is, we specify our array, which must be sorted, and then by - by what field to sort, order - the sort order. Actually that's all. Filtering, I think, it makes no special sense to paint - everything is simple there. It is only necessary to catch our parameter and filter on it.

      For myself, decided so. In component.php in the search component folder (copied to your space) did.

      We take away the full list of goods

       $obSearch->Search($arFilter, $aSort, $exFILTER); 

      Then I get an array of values:

        $ar2 = $obSearch->GetNext(); $i = 0; while($ar2){ $ar2 = $obSearch->GetNext(); if($ar2["ITEM_ID"]){ $fullArr[$i] = $ar2; $rs = CIBlockElement::GetList( array("SORT" => "ASC", "ID" => "ASC"), array( "IBLOCK_ID" => PRODUCTS_IBLOCK_ID, "ID" => $ar2["ITEM_ID"] ), false, false, array("ID","CATALOG_QUANTITY") ); while ($ar = $rs->GetNext()){ $fullArr[$i]['CATALOG_QUANTITY'] = $ar['CATALOG_QUANTITY']; } } $i++; } 

      Sort through usort

        function sortByQuantity($a,$b){ return ((int)$a["CATALOG_QUANTITY"] > (int)$b["CATALOG_QUANTITY"])?-1:1; } usort($fullArr, "sortByQuantity"); 

      And we drive back to CDBResult

       $obSearch ->InitFromArray($fullArr);