Link to home
Start Free TrialLog in
Avatar of scottkern3
scottkern3Flag for United States of America

asked on

PHP sort names by last and first names

The code I have explodes the names into alphabetical order but I need to get it in alphabetical order by last name... note the Mr.

Any help would be appreciated.
<?PHP
$harry_potter_characters = "Bathsheda Babbling, Malcolm Baddock, Mr Bagman, Ludovic Bagman, " .
                                   "Otto Bagman, Millicent Bagnold, Bathilda Bagshot, Heathcote, Barbary, " .
                                   "Musidora Barkwith, Baruffio, Ali Bashir, Hetty Bayliss, Oswald Beamish, " .
                                   "Herbert Beery, Flavius Belby, Marcus Belby, Humphrey Belcher, Katie Bell";
 
class potter_character
{
   var $first_name;
   var $last_name;
}
 
$potter_characters=explode(", " , $harry_potter_characters);
usort($potter_characters, "strcasecmp");
var_dump($potter_characters)
?>

Open in new window

Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Try this:
usort($potter_characters, create_function('$a,$b',
  '$tmpa=explode(" ",$a);
   $tmpb=explode(" ",$b);
   return strcasecmp(
     array_pop($tmpa),
     array_pop($tmpb));'));

Open in new window

Avatar of scottkern3

ASKER

This is the output I get from that...
I was thinking the output should be:

Babbling, Bathsheda
Baddock, Malcolm....
...followed by first name only characters

hopefully that makes sense.
0 => string 'Bathsheda Babbling' (length=18)
  1 => string 'Malcolm Baddock' (length=15)
  2 => string 'Otto Bagman' (length=11)
  3 => string 'Ludovic Bagman' (length=14)
  4 => string 'Mr Bagman' (length=9)
  5 => string 'Millicent Bagnold' (length=17)
  6 => string 'Bathilda Bagshot' (length=16)
  7 => string 'Barbary' (length=7)
  8 => string 'Musidora Barkwith' (length=17)
  9 => string 'Baruffio' (length=8)
  10 => string 'Ali Bashir' (length=10)
  11 => string 'Hetty Bayliss' (length=13)
  12 => string 'Oswald Beamish' (length=14)
  13 => string 'Herbert Beery' (length=13)
  14 => string 'Marcus Belby' (length=12)
  15 => string 'Flavius Belby' (length=13)
  16 => string 'Humphrey Belcher' (length=16)
  17 => string 'Katie Bell' (length=10)
  18 => string 'Heathcote' (length=9)

Open in new window

Is this what you want:
foreach($potter_characters as $name) {
  $nameparts = explode(' ',$name);
  echo array_pop($nameparts);
  if($nameparts)
    echo ', '.implode(' ',$nameparts);
  echo '<br />';
}

Open in new window

Nice! Now the only thing is the first names onlys at the end of the list.

Is that even possible?

Sure, just modify the sorting function. If there are multiple names, sort before those with a single name:
usort($potter_characters, 
  create_function('$a,$b',
  '$tmpa=explode(" ",$a);
   $tmpb=explode(" ",$b);
   $lasta=array_pop($tmpa);
   $lastb=array_pop($tmpb);
   return strcasecmp(
     (count($tmpa)?"1":"2").$lasta,
     (count($tmpb)?"1":"2").$lastb);'));

Open in new window

Your code before this last one had the output with last name, first name... <--- I liked this
this put it back to full name with last name alphabetical like in this (what I pasted in code snippet)

Is there a way I could do it with the "for_each" example above?
 
0 => string 'Bathsheda Babbling' (length=18)
  1 => string 'Malcolm Baddock' (length=15)
  2 => string 'Otto Bagman' (length=11)
  3 => string 'Ludovic Bagman' (length=14)
  4 => string 'Mr Bagman' (length=9)
  5 => string 'Millicent Bagnold' (length=17)
  6 => string 'Bathilda Bagshot' (length=16)
  7 => string 'Musidora Barkwith' (length=17)
  8 => string 'Ali Bashir' (length=10)
  9 => string 'Hetty Bayliss' (length=13)
  10 => string 'Oswald Beamish' (length=14)
  11 => string 'Herbert Beery' (length=13)
  12 => string 'Marcus Belby' (length=12)
  13 => string 'Flavius Belby' (length=13)
  14 => string 'Humphrey Belcher' (length=16)
  15 => string 'Katie Bell' (length=10)
  16 => string 'Barbary' (length=7)
  17 => string 'Baruffio' (length=8)
  18 => string 'Heathcote' (length=9)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That makes a little more sense.

Thank you so much!