?
Solved

JavaScript: Order Array by Last Name

Posted on 2013-06-11
3
Medium Priority
?
297 Views
Last Modified: 2013-06-12
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

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1400 total points
ID: 39239217
within the function, split the name in "a" and in "b" at the spaces and then compare just the last name (the last item after the split(' ')) -see code below:
 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){
 		var last_a=a.split(' ').pop();
		var last_b=b.split(' ').pop();
		return last_a<last_b?-1:1;
		
		}  );
 alert(people.join('\n'))

Open in new window

0
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 600 total points
ID: 39239268
Here we go:
http://jsfiddle.net/EE_RainerJ/ajXyw/

Using RegEx
0
 
LVL 82

Expert Comment

by:hielo
ID: 39239289
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;
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question