sorting drupal profiles by lastname in a view

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;

Add new comment

Markdown

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.