digitalbricks Posted November 22, 2023 Posted November 22, 2023 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: title: "a" title: "B" 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?
da² Posted November 22, 2023 Posted November 22, 2023 Hello, Did you try sort flags? I don't know which one it is, maybe SORT_NATURAL: $repeater->sort('title', SORT_NATURAL); 2
digitalbricks Posted November 22, 2023 Author Posted November 22, 2023 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.
digitalbricks Posted November 22, 2023 Author Posted November 22, 2023 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! 1
da² Posted November 22, 2023 Posted November 22, 2023 @digitalbricks Checking PHP documentation it's necessary to combine 2 flags, both combinations work: $a = ['a','b','C','A']; sort($a, SORT_NATURAL|SORT_FLAG_CASE); // a, A, b, c sort($a, SORT_STRING|SORT_FLAG_CASE); // a, A, b, c EDIT: cross-posted ? 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now