0

"i need to display data in alphabetical oredr if weights are in same number(1,1,1)condition ,if weights like(1,2,3) condition need to display data weight based ASC order" i'm using below code alphabetical sorting working good in same weights condition, but weight based condition not working please help me

               $query = new EntityFieldQuery();

               $query->entityCondition('entity_type', 'node')

              ->entityCondition('bundle', 'closing_team')
              ->fieldCondition('field_homebranch', 'tid',   $node->field_closing_team_categories['und']['0']['tid'])
              ->fieldCondition('field_featured','value','No','=')
              ->propertyOrderBy('title', 'ASC');

              $result = $query->execute();
              $nids = array_keys($result['node']);
              $news_items2 = node_load_multiple($nids);
                /*echo "<pre>";
                print_r($news_items2);
                echo "</pre>";exit;*/

             function element_sort1($a, $b) {
             $a_weight = (is_array($a) && isset($a ['#weight'])) ? $a ['#weight'] : 0;
             $b_weight = (is_array($b) && isset($b ['#weight'])) ? $b ['#weight'] : 0;
             if ($a_weight == $b_weight) {
              return 0;
                }
            return ($a_weight > $b_weight);
                }

?>

0

This is more of a straight PHP question than a Drupal question, but it looks as if you have a bug in your value comparing function. It should return 1 if you want $a to sort greater than $b, it should return -1 for $b to sort greater than $a, and it should return 0 if the items should sort equally. But your function will only ever return 0 or 1. Try replacing your last return statement with return ($a_weight > $b_weight) ? 1 : -1;

0
> Below code working good regarding sort by weight and title both
> condition,by default taken sort by weight ,if all weights are same
> then it  display alphabetical order .


         $query = new EntityFieldQuery();
         $query->entityCondition('entity_type', 'node')
         ->entityCondition('bundle', 'closing_team')
         ->propertyCondition('status', NODE_PUBLISHED)
         ->fieldCondition('field_homebranch', 'tid', $node->field_closing_team_categories['und']['0']['tid'])
         ->fieldCondition('field_featured','value','No','=') 
         ->propertyOrderBy('title', 'DESC');
         $result2 = $query->execute();
          if (isset($result2['node']))
          {
             $news_items_nids2 = array_keys($result2['node']);
             $news_items2 = entity_load('node', $news_items_nids2);
             echo "<pre>";
              print_r($news_items2);
             echo "</pre>";
          }
         function sortByTitle($a, $b)
          {
            return strcmp($a->weight_weight, $b->weight_weight);
          }
          usort($news_items, 'sortByTitle'); 
          function sortByTitle2($a, $b)
          {
           return strcmp($a->weight_weight, $b->weight_weight);
          }
          usort($news_items2, 'sortByTitle2');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.