Jump to content

Emulate natsort


adrian
 Share

Recommended Posts

Hey guys,

Is there any way to emulate PHP's natsort using PW's sort functionality?

I have image filenames with numbered suffixes and of course they aren't ordering correctly with a standard sort.

Thanks

Link to comment
Share on other sites

Thanks Wanze,

Was hoping there was a PW way. I ended up using the following:

$images = $page->$image_field->getArray();
natsort($images);

$reversed_images = array_reverse($images);

$sortedImages = new WireArray();
$sortedImages->import($reversed_images);

$page->$image_field = $sortedImages;

I had to reverse the array, I am guessing because of the way import brings them in, so that they are in the correct ascending order. After the final line I make a few more changes to the $page and then save it.

Anyway, this seems to work fine.

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