2 posts by 2 authors in: Forums > CMS Builder
Last Post: November 17, 2017   (RSS)

Is it possible to sort alphabetically by Surname if a text field contains a full name.

For example:

Dave Grohl
Anthony Kiedis
Gregory Porter
Robert Smith

I think if it could be sorted by 'last word' or 'first word after space' that will be fine. There could be anomalies, with double-barrel names etc, but these will be infrequent and can be dealt with using drag sort order.



It is definitely possible. There are two ways to do that:

1. Sort them after you get the list of records through PHP. You can use function like usort(): http://php.net/manual/en/function.usort.php. And get record's surname by preg_match(). This won't work perfectly if you have pagination setup and don't have the full list of records.

2. Sort them before you get the list of records through MySQL. You might need something like SUBSTRING_INDEX(): https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substring-index

Let me know if you have any questions!

Leo - PHP Programmer (in training)