Sort array of objects by string property value

Asked : Nov 17

Viewed : 96 times

I have an array of JavaScript objects:

var objs = [ 
    { first_nom: 'Lazslo', last_nom: 'Jamf'     },
    { first_nom: 'Pig',    last_nom: 'Bodine'   },
    { first_nom: 'Pirate', last_nom: 'Prentice' }
];

How can I sort them by the value of last_nom in JavaScript?

I know about sort(a,b), but that only seems to work on strings and numbers. Do I need to add a toString() method to my objects?

javascript arrays sorting comparison 
4 Answers

It's easy enough to write your own comparison function:

function compare( a, b ) {
  if ( a.last_nom < b.last_nom ){
    return -1;
  }
  if ( a.last_nom > b.last_nom ){
    return 1;
  }
  return 0;
}

objs.sort( compare );

Or inline (c/o Marco Demaio):

objs.sort((a,b) => (a.last_nom > b.last_nom) ? 1 : ((b.last_nom > a.last_nom) ? -1 : 0))

answered Jan 13


Suppose you have an array users. You may use users.sort and pass a function that takes two arguments and compare them (comparator)

It should return

  • something negative if the first argument is less than second (should be placed before the second in resulting array)
  • something positive if the first argument is greater (should be placed after the second one)
  • 0 if those two elements are equal.

In our case, if two elements are a and b we want to compare a.firstname and b.firstname

Example:

users.sort(function(a, b){
    if(a.firstname < b.firstname) { return -1; }
    if(a.firstname > b.firstname) { return 1; }
    return 0;
})

This code is going to work with any type.

Note that in "real life"™ you often want to ignore cases, correctly sort diacritics, weird symbols like ß, etc when you compare strings, so you may want to use localeCompare. See other answers for clarity.

answered Jan 13


In the above program, the sort() method is used to sort an array by the name property of its object elements., In the above program, the sort() method is used to sort an array element by the age property., The sort() method sorts its elements according to the values returned by a custom sort function (compareName in this case)., In this example, you will learn to write a JavaScript program that will sort an array of objects by property values.

Example 1: Sort Array by Property Name

// program to sort array by property name

function compareName(a, b) {

   // converting to uppercase to have case-insensitive comparison
   const name1 = a.name.toUpperCase();
   const name2 = b.name.toUpperCase();

   let comparison = 0;

   if (name1 > name2) {
      comparison = 1;
   } else if (name1 < name2) {
      comparison = -1;
   }
   return comparison;
}

const students = [{
   name: 'Sara',
   age: 24
}, {
   name: 'John',
   age: 24
}, {
   name: 'Jack',
   age: 25
}];

console.log(students.sort(compareName));

Output

[{
      name: "Jack",
      age: 25
   },
   {
      name: "John",
      age: 24
   },
   {
      name: "Sara",
      age: 24
   }
]

answered Jan 13


sort array of objects by string property value

function compareFirstNames( a, b ) {
  if ( a.first_name < b.first_name ){
    return -1;
  }
  if ( a.first_name > b.first_name ){
    return 1;
  }
  return 0;
}

var people =[
    {"first_name":"Carol", "age":29},
    {"first_name":"Anna", "age":32},
    {"first_name":"Bob", "age":32}
];

people.sort( compareFirstNames ); //people is now sorted by first name from a-z

answered Jan 13


Login and Submit Your Answer
Browse other questions tagged  javascript  arrays  sorting  comparison or ask your own question.