Jump to content

Recommended Posts

Posted

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

Posted

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.

Posted

sinnut is right, although it's using ksort and krsort. Don't know if there's a way around it. How about if you use the page name for sorting?

Posted

Yes, and I most certainly will. Thanks!

Is there a way to treat the fields before sorting, is it possible? For instance, something like sorting a strtoupper(field), or maybe sorting by a sum of 2 fields..

Posted (edited)

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
Posted

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

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
×
×
  • Create New...