Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Sort array of objects by string property value - JavaScript
Sorting an array of objects by a string property is a common task in JavaScript. The most efficient approach uses the built-in sort() method with localeCompare() for proper alphabetical ordering.
Sample Data
Let's work with this array of person objects:
const people = [
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Pirate', last_name: 'Prentice' }
];
console.log("Original array:", people);
Original array: [
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Pirate', last_name: 'Prentice' }
]
Method 1: Using localeCompare() (Recommended)
The localeCompare() method provides proper alphabetical sorting that handles case sensitivity and special characters correctly:
const people = [
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Pirate', last_name: 'Prentice' }
];
// Sort by last_name alphabetically
people.sort((a, b) => a.last_name.localeCompare(b.last_name));
console.log("Sorted by last_name:", people);
Sorted by last_name: [
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pirate', last_name: 'Prentice' }
]
Method 2: Simple String Comparison
For basic sorting, you can use direct string comparison with the less than () operators:
const people = [
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Pirate', last_name: 'Prentice' }
];
// Sort using string comparison
people.sort((a, b) => {
if (a.last_name b.last_name) return 1;
return 0;
});
console.log("Sorted using comparison:", people);
Sorted using comparison: [
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pirate', last_name: 'Prentice' }
]
Reverse Order Sorting
To sort in descending order, simply reverse the comparison:
const people = [
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Pirate', last_name: 'Prentice' }
];
// Sort in descending order
people.sort((a, b) => b.last_name.localeCompare(a.last_name));
console.log("Sorted descending:", people);
Sorted descending: [
{ first_name: 'Pirate', last_name: 'Prentice' },
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pig', last_name: 'Bodine' }
]
Reusable Sorting Function
Create a generic function to sort by any string property:
function sortByProperty(array, property) {
return array.sort((a, b) => a[property].localeCompare(b[property]));
}
const people = [
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Pirate', last_name: 'Prentice' }
];
// Sort by first_name
sortByProperty(people, 'first_name');
console.log("Sorted by first_name:", people);
Sorted by first_name: [
{ first_name: 'Lazslo', last_name: 'Jamf' },
{ first_name: 'Pig', last_name: 'Bodine' },
{ first_name: 'Pirate', last_name: 'Prentice' }
]
Comparison
| Method | Case Sensitive | Handles Special Characters | Best For |
|---|---|---|---|
localeCompare() |
Configurable | Yes | Most sorting scenarios |
| String comparison | Yes | Basic | Simple ASCII sorting |
Conclusion
Use localeCompare() for robust string property sorting in JavaScript. It handles case sensitivity and special characters properly, making it the preferred method for most applications.
