0

I have JSON arrays of objects. I am trying to sort an array using usort. I want to use value field from field_listing_order. It sorted using value. I am missing something but not able to figure it out. please review the code. Thanks!

stdClass Object
(
    [node_title] => abc
    [nid] => 2281
    [field_api_order_value] => 201
    [field_node_entity_type] => node
    [_data] => Array
        (
            [nid] => Array
                (
                    [entity_type] => node
                    [entity] => stdClass Object
                        (

                            [title] => abc
                            [field_listing_order] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] => 8
                                                    [format] => 
                                                    [safe_value] => 8
                                                )

                                        )

                                )

                       )

                 )

        )

)

stdClass Object
(
    [node_title] => abc
    [nid] => 2243
    [field_api_order_value] => 204
    [field_node_entity_type] => node
    [_data] => Array
        (
            [nid] => Array
                (
                    [entity_type] => node
                    [entity] => stdClass Object
                        (

                            [title] => abc
                            [field_listing_order] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] => 3
                                                    [format] => 
                                                    [safe_value] => 3
                                                )

                                        )

                                )

                       )

                 )

        )

) stdClass Object
(
    [node_title] => abc
    [nid] => 2431
    [field_api_order_value] => 242
    [field_node_entity_type] => node
    [_data] => Array
        (
            [nid] => Array
                (
                    [entity_type] => node
                    [entity] => stdClass Object
                        (

                            [title] => abc
                            [field_listing_order] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] => 1
                                                    [format] => 
                                                    [safe_value] => 1
                                                )

                                        )

                                )

                       )

                 )

        )

)

and So on ...

  foreach ($view->result as $result) {   
        $node = $result->_data['nid']['entity'];  
        $listing_order = $node->field_listing_order[LANGUAGE_NONE][0];
         .....            
       // code goes here and it works well. sorting issue
}

 usort ($node->field_listing_order[LANGUAGE_NONE][0], function($a, $b){
       return strcmp($a->value, $b->value);
   }); ?>
2
  • You want to sort elements inside field_listing_order or "global" objects? Commented May 7, 2018 at 18:52
  • It's about sorting global object using value not inside. $a and $b used comparing the value. Commented May 7, 2018 at 19:00

1 Answer 1

1

If you need to sort all the nodes using the field_listing_order value, you need to compare the values along the full path, from the first object to the value:

usort($view->result, function($a, $b) {
    $la = $a->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
    $lb = $b->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
    return $la - $lb ;
});

In this case $a and $b are two different nodes which could be compared. That why you should compare the field_listing_order's value of these nodes. The answer is working with $la- $lb

Sign up to request clarification or add additional context in comments.

6 Comments

Note: this sort should be done outside the loop, of course.
it's not sorting and not seeing any error code either. What if the array is already sorted. Still we can sort using some other element. Right?
Yes, there is no changes if the array is already sorted. You could try $la-$lb to check if the order is reversed. Is it your question?
I am just asking. I will try $la-$lb.
Thank you @Syscall. Finally I have figured it out issue on my end and it's working with $la-$lb
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.