Jump to content

Repeater Field sort() issue


Recommended Posts

Hello community,

using PW 3.0.229 I am experiencing a strage behavior using sort() on repeater fields:
I have a repeater field with an title field and three items populated:

  1. title: "a"
  2. title: "B"
  3. title: "C"

In the template i am calling

$items = $page->repeaterfield->sort('title');

When i now foreach() over $items i get the following sort order:
B, C, a

When i rename "B" to "b" (lowercase) i get this:
C, a, b

When all titles are uppercase, i get what i expect:
A, B, C

So it seems that sort() is kind of case sensitive, sorting items with lowercase titles behind items with uppercase items.
I never stumpled over this before – can anyone confirm this or knows a sorting method that does not take uppercase/lowercase in account?

Link to comment
Share on other sites

I tried the syntax you provided, testing different flags – with no changes in sort behavior.
But you pointed me in the right direction: As the documentation to sort() says, the second argument can be int or null.
So i tried this one:

$items = $page->repeaterfield->sort('title', 1); // note the 1 as second argument

An this worked!

I am still confused to wich sortFlag the 1 is "translated" but at least this solves the issue.

EDIT: I was too hasty – the solution above does NOT work.

Link to comment
Share on other sites

This seems to work:

$items = $page->repeaterfield->sort('title', SORT_NATURAL | SORT_FLAG_CASE);

Got this from Example #2 on PHPs sort() manpage.
So in essence, you where right @da² – i just had to add SORT_FLAG_CASE.

(Until today, i didn't even know that these PHP constants exists. Lesson learned 😉

Thank you!

  • 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

  • Recently Browsing   0 members

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