Jump to content

$page->children("sort.... from smallest to greatest number


Matte85
 Share

Recommended Posts

Hi,

I have encountered the following problem in the otherwise magnificent CMS ProcessWire:

I'm sorting pages according to: "$page->children("sort=...". This is principally working fine, but the problem occurs when values like the following are returned: "10,4m3; 10m3; 8m3...". ProcessWire interprets them as text, which means that it thinks that e.g. "8m3" is greater than "10m3" seeing how the earlier begins on a greater number.

Does anyone have any ideas how I could work around this issue, and indeed get PW to sort the values in the following order: "8m3; 10m3; 10,5m3...".

Thanks!

Link to comment
Share on other sites

I don't think you'll be able to do that in a PW selector, but you can sort it after you have the page array.

I asked this question a long time ago - that thread should help you:

https://processwire.com/talk/topic/3392-emulate-natsort/

I am not sure if natsort works with comma decimals, but maybe there is an alternative php function you can use if needed? 

I am sure one of the european PW devs will have some input if this is an issue.

PS Welcome to the forums!

  • Like 1
Link to comment
Share on other sites

Hi Matte85,

Welcome to PW and the forums...

Technically, this is a PHP issue and not a PW one :-). What you are looking for is PHP's  natsort — Sort an array using a "natural order" algorithm or the related natcasesort — Sort an array using a case insensitive "natural order" algorithm. The implementation of the former can be seen in the following posts:

https://processwire.com/talk/topic/3392-emulate-natsort/

https://processwire.com/talk/topic/3805-trying-to-sort-by-number-in-string/

https://processwire.com/talk/topic/6144-how-to-natural-sort-a-pagearray/

https://processwire.com/talk/topic/4587-sort-images-by-title-in-image-inputfield/

A shortcoming is that you probably want to grab a limited number of children based on the natural sort, rather than sorting after grabbing them...Not sure how to overcome this.

Edit:

I am late to the party as usual! Beaten by @Adrian :-)

Edited by kongondo
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...