Next & Previous in a filtered file collection

Hello,
i would like to get the next item in a custom file collection. i have passed some filter infos through the params and can build the collection on the display page.
however, i can’t get the next item from that collection, as it is ignoring the filtering and instead always shows the next item from the folder where the image sits.
the images are spread out in different folders, so i get an error message each time it is the last image in the folder.
so, my question is: is there a way to get the next item in a custom file collection that spreads across different folders?

Thanks in advance.
Matthias

These two custom methods will help you:

// get next page of a given children collection (e.g. filtered collection)
page::$methods['getNext'] = function($page, Children $siblings, $sort = array(), $visibility = false) {
  if($sort) $siblings = call(array($siblings, 'sortBy'), $sort);
  $index = $siblings->indexOf($page);
  if($index === false) return null;
  if($visibility) {
    $siblings = $siblings->offset($index+1);
    $siblings = $siblings->{$visibility}();
    return $siblings->first();
  } else {
    return $siblings->nth($index + 1);
  }
};
// get prev page of a given children collection (e.g. filtered collection)
page::$methods['getPrev'] = function($page, Children $siblings, $sort = array(), $visibility = false) {
  if($sort) $siblings = call(array($siblings, 'sortBy'), $sort);
  $index = $siblings->indexOf($page);
  if($index === false or $index === 0) return null;
  if($visibility) {
    $siblings = $siblings->limit($index);
    $siblings = $siblings->{$visibility}();
    return $siblings->last();
  } else {
    return $siblings->nth($index - 1);
  }
};

Save in a plugin file, e.g. /site/plugins/methods.php and you are ready to use them.

Thanks a lot for that!
Very helpful. As always…

Matthias