Javascript Array sort by last name, first name

sort by last name then first name javascript
javascript sort array
javascript sort array of numbers
javascript sort array by regex
javascript sort by then by
javascript sort array of objects by multiple properties
javascript sort return
sort associative array javascript

I have the following array in JavaScript, I need to sort them by last name.

var names = [Jenny Craig, John H Newman, Kelly Young, Bob];

Results would be:

Jenny Craig, 
John H Newman, 
Kelly Young 

Any examples on how to do this?

Try this:

function compare(a, b) {
    var splitA = a.split(" ");
    var splitB = b.split(" ");
    var lastA = splitA[splitA.length - 1];
    var lastB = splitB[splitB.length - 1];

    if (lastA < lastB) return -1;
    if (lastA > lastB) return 1;
    return 0;

var names = ["John H Newman", "Jenny Craig", "Kelly Young", "Bob"];
var sorted = names.sort(compare);

Here's a Fiddle.

Tutorial: Sort by Last Name with Javascript, First, before we sort by last name, let's look at how the sort method works As you can see the default behaviour of sort() is to order the array  First, before we sort by last name, let’s look at how the sort method works by default, by sorting our players by first name: // First let's get the string of names var submittedPlayers = "Lionel Messi, Cristiano Renaldo, Arjen Robben, Luis Suarez, Manuel Neuer, Gareth Bale" // then we'll break the string into an array var playersArray = submittedPlayers.split(", "); // and sort the array playersArray.sort(); // and finally, let's see what we've got console.log(playersArray); // ["Arjen

function lastNameSort(a,b) {
    return a.split(" ").pop()[0] > b.split(" ").pop()[0]

This was inspired by this answer.

JavaScript Sorting, Many of you are aware of the built-in sort function for sorting JavaScript arrays. to sort the array of first names, but keep the corresponding positions in the last  The sort() method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort() method sorts the values as strings in alphabetical and ascending order. This works well for strings ("Apple" comes before "Banana").

Try This:

function sortContacts(names, sort) {

    if(sort == "ASC")
        return names.sort(lastNameSort);    
        return names.sort(lastNameSortDesc);    


function lastNameSort(a,b) {  
    if(a.split(" ")[1] > b.split(" ")[1])
      return 1;
      return -1;

function lastNameSortDesc(a,b) {  
    if(a.split(" ")[1] > b.split(" ")[1])
      return -1;
      return 1;

For your knowledge, the Javascript's SORT function allows us to send a "Compare" method in the parameter. In the above code, it's using the same technique.

How to do complex sorting in Javascript, This blog post contains a tutorial on complex sorting in Javascript var array = [ { firstname: 'Jack', lastname: 'Miller', age: 25, gender: 'm' }  You can use String class's substring () method to get last names and then you can make a new array of last names and then you can sort that array writing logic And, if i input

Sorting an Array in JavaScript, Learn how to use the built-in `Array#sort()` function in JavaScript. const characters = [ { firstName: 'Jean-Luc', lastName: 'Picard', rank:  The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

Sort an Array of Objects by Values of Object's Properites in JavaScript, Suppose that you have an array of employee objects as follows: let employees = [ { firstName: 'John', lastName: 'Doe', age: 27, joinedDate: 'December 15, 2017' }  You want to render this list, but first you want to order it by the value of one of the properties. For example you want to order it by the color name, in alphabetical order: black, red, white. You can use the sort() method of Array, which takes a callback function, which takes as parameters 2 objects contained in the array (which we call a and b):

First And Last Name With Last Name Alphabetized In Javascript , Created the same way you currently make "namesList" array, just store in the display order (first last or last, first) desired. Then you can sort  Create an array styles with items “Jazz” and “Blues”. Append “Rock-n-Roll” to the end. Replace the value in the middle by “Classics”. Your code for finding the middle value should work for any arrays with odd length. Strip off the first value of the array and show it. Prepend Rap and Reggae to the array. The array in the process:

  • do you want to sort them alphabetically, or just put those without last names first?
  • Is Bob a first or a last name? Does the H belong to the first or the last name?
  • Amir answer worked for me.
  • @Jeffrey: if Amir's answer worked for you, then please do consider accepting it :)
  • That worked, now I have to loop it and show the results in a ui. :)
  • @JeffreyMessick you should mark this answer as accepted :)