Jump to content

sort pagearray on calculated values


Spica
 Share

Recommended Posts

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

That would require an in-memory sort. One way to do it (as we wait for @LostKobrakai's better answer :P )....

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:

  1. X-Small: distance - 84 Miles
  2. XX-Large: distance - 84 Miles
  3. Small: distance - 77 Miles
  4. US Open: distance - 75 Miles
  5. About Us: distance - 74 Miles
  6. Our Achievements: distance - 69 Miles
  7. Key Staff: distance - 65 Miles
  8. Who We Are: distance - 57 Miles
  9. X-Large: distance - 57 Miles
  10. Senior Management: distance - 51 Miles
  11. Medium: distance - 41 Miles
  12. Sizes: distance - 39 Miles
  13. Colours: distance - 25 Miles
  14. Large: distance - 10 Miles
  15. What We Do: distance - 9 Miles

Useful reading: PageArray

Edited by kongondo
Edit: slow again! I told you he'd give you a great answer :-)
  • Like 6
Link to comment
Share on other sites

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

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.

  • Like 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...