Jump to content

WireArray sorts automatically according to page ID. Even if add() or append() methods are used.


Recommended Posts

Hello!
I am building a searching mechanism where the search key will be compared to three different fields in heirarchy and the matching search results will be saved to a PageArray. Like below

$pageData = new PageArray();
$pageData->append($page->children('field_1*=' . $search_key ));
$pageData->append($page->children('field_2*=' . $search_key ));
$pageData->append($page->children('field_3*=' . $search_key ));
$pageData = $pageData->unique();

What I need, is the results of the first query to show up first, then the second and then the third. But the resulted PageArray looks something like the below when printed:

ProcessWire\PageArray Object ( 
  [count] => 11 
  [items] => Array ( 
    [Page:6] => Array ( [id] => 25468 [name] => forage-grass [parent] => /reports/ [template] => report_detailed_page [title] => 3D Forage Grass ) 
    [Page:7] => Array ( [id] => 29724 [name] => 3d-laser-scanners [parent] => /reports/ [template] => report_detailed_page [title] => 3D Laser Scanners) 
    [Page:0] => Array ( [id] => 34700 [name] => 3d-art [parent] => /reports/ [template] => report_detailed_page [title] => 3D art)
    [Page:8] => Array ( [id] => 34711 [name] => 3d-paint [parent] => /reports/ [template] => report_detailed_page [title] => 3D Paint)
    [Page:1] => Array ( [id] => 34734 [name] => 3d-mapping-and-modelling [parent] => /reports/ [template] => report_detailed_page [title] => 3D Mapping and Modelling) 
    [Page:2] => Array ( [id] => 34746 [name] => 3d-secure-pay [parent] => /reports/ [template] => report_detailed_page [title] => 3D Secure Pay) 
    [Page:3] => Array ( [id] => 35930 [name] => 3d-food-printing [parent] => /reports/ [template] => report_detailed_page [title] => 3D Food Printing ) 
    [Page:4] => Array ( [id] => 37142 [name] => 3d-mapping-and-modelling [parent] => /reports/ [template] => report_detailed_page [title] => 3D Mapping and Modelling) 
    [Page:5] => Array ( [id] => 37154 [name] => 3d-secure-pay-authentication [parent] => /reports/ [template] => report_detailed_page [title] => 3D Secure Pay Authentication) 
  ) 
  [selectors] => 
)

As you can see, the pages are sorted according to their IDs. While the keys in the array show the actual order in which they were added to the PageArray.

I tried to use the sort() method but could not get it to work.

I want to print these according to the order they were added to the PageArray. 

Please help me understanding what might be wrong or what I should be doing! Thank you!

Link to comment
Share on other sites

Hi @GradDev,

The $pageData = $pageData->unique() line is redundant because WireArrays/PageArrays never contain duplicates unless you specifically allow them using setDuplicateChecking(false).

I can't reproduce the issue here - the order of items in a PageArray is the same as the order I add them in and they are not ordered by ID. In my example I am adding countries:

2022-07-04_101700.png.9db596ab14e3198a5b355b4753b83bd7.png

But you can see that when adding an item that already exists in the PageArray, the earlier item is removed - Finland, Iceland and Ireland are added in the last append() but would already exist in the PageArray. If you wanted pre-existing items to maintain their earlier position in the PageArray you could modify the selectors so that items in the PageArray are excluded:

2022-07-04_102523.png.d48c5087d6ad3427a2678607713dbe12.png

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...