Imagine you have an array of objects:
People = [
{ "id": 1, "name": Joseph, function: "preacher"},
{ "id": 2, "name": Ann, function: "singer"},
{ "id": 3, "name": Miles, function: "preacher"},
{ "id": 4, "name": Jack, function: "singer"},
{ "id": 5, "name": Igor, function: "secretary"}
];
Additionally, there is an array of properties:
sortingProperties = ['function', 'name'];
The task at hand is to sort the People array using a combination of properties specified in the sorting array. Here's how it can be achieved:
const intlCollator = new Intl.Collator('pt-BR', { usage: 'sort' });
People.sort(
(x, y) =>
(intlCollator.compare(x[sortingProperties[0]], y[sortingProperties[0]])) ||
(intlCollator.compare(x[sortingProperties[1]], y[sortingProperties[1]]))
);
To make the sorting process dynamic and iterate through variable sort combinations, one might do the following:
sortingProperties = ['function', 'name'];
Or try this:
sortingProperties = ['name'];