Sorting
2 posts by 2 authors in: Forums > CMS Builder
Last Post: March 4, 2014 (RSS)
By nmsinc - March 1, 2014
I need to create an orderBy alpha sort for a single field
The field name is: 'name'
This field contains multiple name structures such as: Billy T Kid or Billy The Kid or Billy Kid or Mrs. Pat W. Jones
I need to sort alphabetical by the Last name in the file! -Any help would be appreciated!
Thanks - nmsinc
By gregThomas - March 4, 2014
Hi nmsinc,
I've done some research and it's possible to sort by the last word in a string in a field like this:
'orderBy' => "substring_index(name, ' ', -1) "
So in this case the field I'm using is name. It works by breaking up the name field into an array separated on the space character, getting the last element from the array, and sorting on it.
I'd highly recommend creating separate first name/last name/ title fields. This will make the data a lot easier to manage, and any queries you make on the data much faster.
Let me know if you have any questions.
Thanks!
Greg
PHP Programmer - interactivetools.com