drupal php views sort lastname

I have a view of profile data that I want to sort by lastname. The name field currently has "firstname [middlename] lastname" in it. I looked around and found this example of using views php filter to sort based on a query.

I then made a query which ordered nid's by lastname and plugged that into my view as a filter.


SELECT nid,title,SUBSTRING_INDEX(title,' ',-1) AS lastname FROM node WHERE type='profile' ORDER BY lastname;

Running that query returns the…