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
Selected Reading
Sorting an associative array in ascending order - JavaScript
In JavaScript, you can sort an array of objects (associative array) in ascending order using the sort() method with a custom comparison function.
Suppose we have an array of objects like this:
const people = [
{"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"},
{"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"},
{"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"},
{"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"}
];
We need to sort this array by the age property in ascending order. The expected output should be:
[
{"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"},
{"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"},
{"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"},
{"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"}
]
Using sort() with Custom Comparison Function
The sort() method modifies the original array. We pass a comparison function that returns:
- Negative value if first element should come before second
- Positive value if first element should come after second
- Zero if elements are equal
const people = [
{"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"},
{"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"},
{"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"},
{"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"}
];
// Comparison function for ascending order
const sorter = (a, b) => {
return a.age - b.age;
};
// Function to sort array by age
const sortByAge = arr => {
arr.sort(sorter);
};
sortByAge(people);
console.log(people);
[
{ id: 3, name: 'Christine', age: 20, gender: 'm', category: 'G' },
{ id: 2, name: 'Brandon', age: 25, gender: 'm', category: 'G' },
{ id: 4, name: 'Elena', age: 29, gender: 'W', category: 'M' },
{ id: 1, name: 'Andrew', age: 30, gender: 'm', category: 'G' }
]
Alternative Approaches
You can also write the sorting logic more concisely:
const people2 = [
{"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"},
{"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"},
{"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"},
{"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"}
];
// Direct inline comparison
people2.sort((a, b) => a.age - b.age);
console.log(people2);
[
{ id: 3, name: 'Christine', age: 20, gender: 'm', category: 'G' },
{ id: 2, name: 'Brandon', age: 25, gender: 'm', category: 'G' },
{ id: 4, name: 'Elena', age: 29, gender: 'W', category: 'M' },
{ id: 1, name: 'Andrew', age: 30, gender: 'm', category: 'G' }
]
Sorting by Different Properties
You can sort by any property using the same approach:
const people3 = [
{"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"},
{"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"},
{"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"},
{"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"}
];
// Sort by name alphabetically
people3.sort((a, b) => a.name.localeCompare(b.name));
console.log("Sorted by name:", people3[0].name, people3[1].name);
Sorted by name: Andrew Brandon
Key Points
- The
sort()method modifies the original array - For numbers: use
(a, b) => a.property - b.propertyfor ascending order - For strings: use
(a, b) => a.property.localeCompare(b.property) - For descending order: reverse the comparison
(a, b) => b.property - a.property
Conclusion
Use the sort() method with a custom comparison function to sort arrays of objects by any property. The pattern (a, b) => a.property - b.property provides ascending numeric sorting.
Advertisements
