How to sort an array which elements are objects?
To sort an array of objects, you can use the sort method in JavaScript, or similar methods in other programming languages, by providing a custom comparison function. This function will specify how to sort the objects based on one or more of their properties.
Here's an example in JavaScript:
const array = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 20 },
{ name: "Charlie", age: 30 }
];
// Sort by age in ascending order
array.sort((a, b) => a.age - b.age);
console.log(array);
In this example, we sort array by the age property in ascending order. The comparison function (a, b) => a.age - b.age returns a negative, zero, or positive number to indicate the order of a and b:
A negative value means a comes before b.
Zero means a and b are equal in terms of sorting.
A positive value means b comes before a.
Sorting by Multiple Properties
You can also sort by multiple properties, such as sorting by age first and then by name if ages are the same:
array.sort((a, b) => {
if (a.age === b.age) {
return a.name.localeCompare(b.name); // Sort by name if age is the same
}
return a.age - b.age; // Otherwise, sort by age
});
console.log(array);
Sorting in Descending Order
For descending order, simply reverse the comparison:
array.sort((a, b) => b.age - a.age);
These approaches apply to most programming languages, though the syntax might vary slightly depending on the language.
How to do multi-level sorting if objects have multiple properties?
For multi-level sorting, you can chain comparisons in your sorting function. This approach will evaluate each property in sequence, moving to the next property only if the previous one results in equality.
Here’s a general approach in JavaScript:
const array = [
{ name: "Alice", age: 25, salary: 50000 },
{ name: "Bob", age: 25, salary: 70000 },
{ name: "Charlie", age: 30, salary: 60000 },
{ name: "David", age: 25, salary: 50000 }
];
// Sort by age (ascending), then by salary (descending), and then by name (ascending)
array.sort((a, b) => {
if (a.age !== b.age) {
return a.age - b.age; // Sort by age (ascending)
}
if (a.salary !== b.salary) {
return b.salary - a.salary; // Sort by salary (descending)
}
return a.name.localeCompare(b.name); // Sort by name (ascending)
});
console.log(array);
Explanation
Primary Sort - Sort by age in ascending order: a.age - b.age.
Secondary Sort - If age is the same, sort by salary in descending order: b.salary - a.salary.
Tertiary Sort - If both age and salary are the same, sort by name in ascending order: a.name.localeCompare(b.name).
Using a Dynamic Multi-Level Sort Function
If you have many properties or want a reusable function, you can create a dynamic multi-level sort function like this:
function multiLevelSort(array, ...sortCriteria) {
return array.sort((a, b) => {
for (const [property, order] of sortCriteria) {
const direction = order === "asc" ? 1 : -1;
if (a[property] < b[property]) return -1 * direction;
if (a[property] > b[property]) return 1 * direction;
// Continue to next property if values are equal
}
return 0; // All properties are equal
});
}
const sortedArray = multiLevelSort(
array,
["age", "asc"], // Sort by age (ascending)
["salary", "desc"], // Then by salary (descending)
["name", "asc"] // Then by name (ascending)
);
console.log(sortedArray);
This multiLevelSort function takes an array and a list of [property, order] pairs, making it flexible to handle various sorting orders and properties.
No comments:
Post a Comment