Search the Community
Showing results for tags 'sort()'.
-
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!
-
A recent post on PageArray find() made me try and fix a couple of things discovered there (and in some earlier posts, I think). What I came up with is a few modifications to WireArray class (base class for PageArrays): sort() accepts multiple fields to sort by. Fields can be given as a comma separated string "-modified, title" or an array of strings array("-modified", "title") find() supports start=n and multiple sort=field selectors some optimizations for sorting and limiting even quite big arrays without losing performance in certain conditions The new implementation seems to give same results as the old one where applicable (given only one field to sort by and no start selector for find()), but more tests are definitely needed. I've tested this on a site with more than 15000 pages and find()'ing 3000 of them at once for sorting/finding with the API calls. Performance is about the same as it was before. Even giving more than one field to sort by doesn't make it noticeably slower if limit is being used as well (those optimizations actually work). The replacement for wire/core/Array.php is attached if anyone's interested in trying it out. Array.php I tried some different methods of sorting as well, but just to find out Ryan had a good reason for all the little things I couldn't get a hold of at first . PHP sorting methods being unstable must have turned much of my hair gray in the last couple of days... While testing this I found out that sorting by a field with empty values at some records would act differently when done at initial $pages->find() (database query that is) compared to $aPageArray->find(). While this isn't affected by the WireArray code, I may dig deeper into it later. Giving sort=random works as it did before, but the code doesn't handle the situation it's not the only sort field given. Left a TODO-note in the comments on this (and the missing trackChange() call as well). @ryan: I will try to make a pull request as well just for the fun of it (being new to git). Do whatever you like with the code if you see any potential in it. You may want to rename function stableSort() and its last argument to something else at least. I know I'm not too happy with them.