a-ok Posted October 29, 2016 Share Posted October 29, 2016 Is it possible, when sorting pages by 'name' to place any numerical 'names' to the bottom, rather than at the top (first)? Link to comment Share on other sites More sharing options...
FrancisChung Posted October 29, 2016 Share Posted October 29, 2016 You'll probably have to create a pseudo name sort field and populate that pseudo name field with the desired sort values and sort by that field .... 1 Link to comment Share on other sites More sharing options...
horst Posted October 29, 2016 Share Posted October 29, 2016 sorting descendend, and not ascendend? Link to comment Share on other sites More sharing options...
fbg13 Posted October 30, 2016 Share Posted October 30, 2016 @oma I assume you want them ordered ascending and the ones starting with a number at the end. You could do this by storing the numerical names in a variable and echoing that variable after the "normal" names. $numericalNames = ""; $normalNames = ""; $names = $pages->find("template=template_name"); foreach($names as $n) { /* get the first letter of the current name */ $firstLetter = substr($n->name, 0, 1); $regexPattern = "/[0-9]/"; // if first letter is a number if(preg_match($regexPattern, $firstLetter)) { $numericalNames .= $n->name; } else { $normalNames .= $n->name; } } echo $normalNames; echo $numericalNames; 2 Link to comment Share on other sites More sharing options...
BitPoet Posted October 30, 2016 Share Posted October 30, 2016 If you only want to sort in-memory, getting the pages as a plain array and using a custom sort function should do the trick: <?php $ps = $pages->find('yourselector')->getArray(); usort($ps, function($a, $b) { $na = $a->name; $nb = $b->name; if(!ctype_digit($na) && !ctype_digit($nb)) return strcmp($na, $nb); if(ctype_digit($na) && ctype_digit($nb)) return strcmp($na, $nb); return (ctype_digit($na)) ? 1 : -1; }); foreach($ps as $p) echo $p->name . PHP_EOL; 6 Link to comment Share on other sites More sharing options...
a-ok Posted October 31, 2016 Author Share Posted October 31, 2016 Thanks, folks! Has anyone else had this 'issue', or is it generally common practice to have numbers first, rather than last? I wonder if it would be possible to suggest an PW API avaliable for an option like this? Link to comment Share on other sites More sharing options...
szabesz Posted October 31, 2016 Share Posted October 31, 2016 (edited) 1 hour ago, oma said: generally common practice to have numbers first, I think the answer to it is yes: https://en.wikipedia.org/wiki/ASCII#Character_order Historically, numbers are listed first in dictionaries (real books) as well, at least in Hungarian books Edited October 31, 2016 by szabesz typo 4 Link to comment Share on other sites More sharing options...
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