• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

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

0
scottkern3
Asked:
scottkern3
  • 4
  • 4
1 Solution
 
Roger BaklundCommented:
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

0
 
scottkern3Author Commented:
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

0
 
Roger BaklundCommented:
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

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Is that even possible?

0
 
Roger BaklundCommented:
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

0
 
scottkern3Author Commented:
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

0
 
Roger BaklundCommented:
The last code snippet from me was just the sorting, it does not do output.
<?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";
 
$potter_characters=explode(", " , $harry_potter_characters);
 
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);'));
 
foreach($potter_characters as $name) {
  $nameparts = explode(' ',$name);
  echo array_pop($nameparts);
  if($nameparts)
    echo ', '.implode(' ',$nameparts);
  echo '<br />';
}
 
?>

Open in new window

0
 
scottkern3Author Commented:
That makes a little more sense.

Thank you so much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now