php alphabetically order an array by last word in string

arrays, php

Solution

$names = array(
    'John Doe',
    'Tom Watkins',
    'Jeremy Lee Jone',
    'Chris Adrian',
);

usort($names, function($a, $b) {
    $a = substr(strrchr($a, ' '), 1);
    $b = substr(strrchr($b, ' '), 1);
    return strcmp($a, $b);
});

var_dump($names);

Online demo: http://ideone.com/jC8Sgx

Problem

I have an array. eg: ``` names = { 'John Doe', 'Tom Watkins', 'Jeremy Lee Jone', 'Chris Adrian' } ``` And I want to order it alphabetically by last name(last word in string). Can this be done?

Original source

Related problems