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
Solved

JavaScript: Order Array by Last Name

Posted on 2013-06-11
3
294 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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 350 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 150 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I make a javascript to show drop down 4 22
Asp in script 6 37
Running a javascript project in WebStorm 2 25
Button on Table, name table1 not working 4 21
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

856 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