Imagine you have an array of Person objects:
var people = [{name: "Joe Schmo", age: 36}, {name: "JANE DOE", age: 40}];
and there's a function that can sort an array of strings in a case-insensitive manner:
function caseInsensitiveSort(arr) { ... }
Is there a simple way to integrate the existing sort function with Array.prototype.map
to sort the people
array based on the name
key only?
This would result in:
var people = [{name: "JANE DOE", age: 40}, {name: "Joe Schmo", age: 36}];
While it can be manually achieved like this:
people.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
I'm unable to think of a way to utilize the pre-existing sort function efficiently. This could be particularly useful when dealing with more customized sorting functions.
It seems challenging to determine the original indices after sorting the proxy array using the native JS sort
function. This limitation makes it difficult to achieve the desired outcome effectively.
In my attempts at solving this, I realized the approach was inefficient. Thankfully, a solution utilizing a comparison function is presented below.