sorting drupal profiles by lastname in a view

By thomas, 26 July, 2012

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 nids in lastname order.
So my whole filter is the following:

$sql="select nid,title,substring_index(title,' ',-1) as lastname from node where type='profile' order by lastname;";
$sql = db_rewrite_sql($sql);
$result = db_query($sql);
while ($row = db_fetch_array($result)) {
$node_ids[] = $row['nid'];
}
return $node_ids;