Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

JavaScript: Order Array by Last Name

How can I order this list by last name?

This does NOT work:

 var str = 'X. Michael Goo,Suzy M. Kat,Sal M. Haircut,Scott N. Yuu,Larry B. Huff,Geof L. Mack,David A. Xaa,Brian C. Fuu';
 var people = str.split(',');
 people = people.sort(function(a,b){return a<b;});
 alert(people.join('\n'))

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna 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
SOLUTION
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
NOTE: On my first post, if you prefer the sort order to be in descending order then change:
return last_a<last_b?-1:1;

so that the 1 and -1 are swapped:
return last_a<last_b?1:-1;