Jump to content

PageArray string sorting bug?


tiagoroldao
 Share

Recommended Posts

First of all, my congratulations to Ryan on this amazing piece of software! This is my first post in these forums, but I have been a silent noob for a few months, learning while using processwire for every web project in the Multimedia Design course I'm currently finishing!

On to the bug/problem (is this the right place for this post?) :

When I try to sort a PageArray object using the $a->sort() method on a string field, say, the title, the API places the ones starting with uppercase first, and the lowercase last, like so: A,B,C,D,a,b,c,d...

Am I doing something wrong/missing something?

Cheers,

Tiago

Link to comment
Share on other sites

I can't look it up in the PW code right now but assuming $a->sort is the default php sort it's not a bug. PHP sort function is case sensitive and goes A-Z first and then a-z.

Look at http://php.net/manua...nction.sort.php for more details.

- edit -

https://github.com/r.../Array.php#L657

So ksort and krsort (for reverse)

Maybe it is possible to add options for case-insensitive and natural in the core, something like:

uksort($sortable, "strcasecmp"); // case-insensitive
uksort($sortable, "strnatcasecmp"); // case-insensitive, natural order algorithm

Not sure if Ryan finds it desirable, maybe there are some pitfalls to consider.

Link to comment
Share on other sites

Not tested, but cant't you store your custom sortfield temporary in your pages and sort them later by this field?

Something like this:

foreach ($yourpagearray as $p) {
 $p->sortfield = toLowerCase($p->title);
}
$yourpagearray->sort("sortfield");

EDIT: this code should work:

foreach ($yourpagearray as $p) {
 $p->tempsort = strtolower($p->title);
}
$yourpagearray->sort("tempsort");
Edited by interrobang
  • Like 3
Link to comment
Share on other sites

Yes and no, interrobang :) You're right, and I just solved it using your solution... But you seem to have stumbled into a protected field, maybe? Using "sortfield" as a temporary field throws an error, renaming it to something like "mysortfield" makes it work like a charm!

EDIT: so sortfield it's the sort setting for a page's children. :)

  • Like 2
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

×
×
  • Create New...