Jump to content


Photo

PageArray string sorting bug?

PageArray sort issue API

  • Please log in to reply
5 replies to this topic

#1 tiagoroldao

tiagoroldao

    Jr. Member

  • Members
  • PipPip
  • 19 posts
  • 5

  • LocationPortugal

Posted 22 June 2012 - 05:18 AM

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

#2 SiNNuT

SiNNuT

    Sr. Member

  • Members
  • PipPipPipPip
  • 367 posts
  • 232

Posted 22 June 2012 - 06:10 AM

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.

#3 Soma

Soma

    Hero Member

  • Moderators
  • 3,204 posts
  • 1754

  • LocationSH, Switzerland

Posted 22 June 2012 - 06:45 AM

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?

@somartist | modules created | support me, flattr my work flattr.com


#4 tiagoroldao

tiagoroldao

    Jr. Member

  • Members
  • PipPip
  • 19 posts
  • 5

  • LocationPortugal

Posted 22 June 2012 - 09:58 AM

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..

#5 interrobang

interrobang

    Distinguished Member

  • Members
  • PipPipPipPip
  • 108 posts
  • 49

  • LocationMunich, Germany

Posted 22 June 2012 - 10:14 AM

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, 22 June 2012 - 11:49 AM.


#6 tiagoroldao

tiagoroldao

    Jr. Member

  • Members
  • PipPip
  • 19 posts
  • 5

  • LocationPortugal

Posted 22 June 2012 - 11:09 AM

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. :)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users