Spica Posted April 12, 2016 Share Posted April 12, 2016 I have a pagearray, each page has a location. I calculate the distance to a given location for each page's location. How would I best organize a sort of the pagearray on the distance. The distance is not a field of the pages but calculated. Hope I am clear enough. Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 12, 2016 Share Posted April 12, 2016 You can sort by runtime fields as well. You'd just need to make sure to populate it before sorting. 1 Link to comment Share on other sites More sharing options...
kongondo Posted April 12, 2016 Share Posted April 12, 2016 (edited) That would require an in-memory sort. One way to do it (as we wait for @LostKobrakai's better answer ).... Create and populate a property 'distance' on the fly for each page in the PageArray then sort the array by that property. An example: // get a PageArray $res = $pages->find('template=basic-page, limit=15'); // assign distances to a property 'distance' created on the fly foreach ($res as $r) { $r->distance = rand(5, 100);// we assign some random distance just for testing } // testing output $out = ''; // sort #$res->sort('distance');// sort ascending $res->sort('-distance');// sort descending // confirm sorting worked $out .= '<ol>'; foreach ($res as $r) { $out .= '<li>' . $r->title . ': distance - ' . $r->distance . ' Miles</li>'; } $out .= '</ol>'; // it works! echo $out; You should get results similar to this: X-Small: distance - 84 Miles XX-Large: distance - 84 Miles Small: distance - 77 Miles US Open: distance - 75 Miles About Us: distance - 74 Miles Our Achievements: distance - 69 Miles Key Staff: distance - 65 Miles Who We Are: distance - 57 Miles X-Large: distance - 57 Miles Senior Management: distance - 51 Miles Medium: distance - 41 Miles Sizes: distance - 39 Miles Colours: distance - 25 Miles Large: distance - 10 Miles What We Do: distance - 9 Miles Useful reading: PageArray Edited April 12, 2016 by kongondo Edit: slow again! I told you he'd give you a great answer :-) 6 Link to comment Share on other sites More sharing options...
Spica Posted April 13, 2016 Author Share Posted April 13, 2016 Ah, thanks, kongondo, that was the missing link / information for me. I now just wonder about the best sort strategie. I have different sort options beside the distance to sort the pagearray. All given by a users selection. So all other sort options are executed within the selector in $pages->find. Distance cannot be sorted within find. Should I mix find(sort=) with $res->sort() routines or do a routine just with $res->sort() for all sort options. Are there any reasons to prefere a sorting within find() over sort()? Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 13, 2016 Share Posted April 13, 2016 If you need to use pagination (limit=XX) for the resulted pages you'd need to resort to something like this: https://github.com/ryancramerdesign/ProcessWire/issues/556 If you're not using limit in your selector I'd suggest using sort in the $pages->find() selector if possible and fall back to runtime sorting only if needed. Edit: The difference between find() and sort() is, that find() will sort results in mysql and therefore (in conjuction with limit) will only load actually displayed pages, whereas sort() does only sort pages in php (loaded to memory), which might draw a lot of performance if done on a large set of pages. 1 Link to comment Share on other sites More sharing options...
Spica Posted April 13, 2016 Author Share Posted April 13, 2016 Good Point, LostKobrakai, actually I need a pagination. So sort() seems not to be the right way. Will check your link, at first glance it seems more compicated than expected. Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 13, 2016 Share Posted April 13, 2016 That's mostly owing the fact that the distance calculation must be done in sql syntax. Link to comment Share on other sites More sharing options...
Spica Posted April 13, 2016 Author Share Posted April 13, 2016 I found an fork of FieldtypeMapMarker I already worked with that already does the sorting in the sql query. Think that could fit my needs. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now