Jump to content

Recommended Posts

Posted

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

Posted

Without emulating :)

$imgArray = $images->getArray();
natsort($imgArray);
// continue with $imgArray or...
$images = new WireArray();
$images->import($imgArray);

Not tested ;)

  • Like 2
Posted

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

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
  • Recently Browsing   0 members

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