Jump to content

Recommended Posts

Posted

I'm trying to sort a PageArray by $page->name and because there are digits in the name I need to use a natural sort (like php's natsort()).

I've gone through the process outlined in https://processwire.com/talk/topic/3392-emulate-natsort/ but I must be missing something because I can't get it to work at all.

I then tried a different way and created an array of the page names, used natsort and implode to give me a correctly sorted string to use in a selector.  I then do $output = $pages->find("name=$selector_text"); and the correct pages are returned but they're not in the same order as in my selector string.

I've only found a couple mentions of natural sorting on the forums so I must be missing something simple but I just can't put my finger on it.

Posted

The method on that page you linked to should work just fine, but obviously will need tweaking for a PageArray, rather than a WireArray of images.

Show us the code you are using so we can debug that for you.

Posted

Fixed it and cleaned it up this morning.  Here's the function should anyone have any ideas for streamlining it or wanders across this thread searching for a possible solution:

function natural_sort_pagearray($unsorted_array)
{
	$temp_array = array();

	foreach($unsorted_array as $page) {
		$temp_array[] = $page;
	}

	usort($temp_array, function($a, $b)
	{
		return strnatcmp($a->name, $b->name);
	});

	$sorted_array = new PageArray();
	$sorted_array->import($temp_array);
	return $sorted_array;
}
Posted

i have same problem with my pageArray, its get meessed up after :
 

$pageArray= $pages->find('template=news, limit=25');

Cant find solution to this.

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
×
×
  • Create New...