After posting this question regarding sorting an array with a key function, it became evident that utilizing a comparison function was inevitable.
The challenge at hand includes:
- Having a resource-intensive key function that needs to be transformed into a comparison function
- Dealing with an array of objects, restricting the use of a hash table for memoizing key function results
Below is a demonstration of a sample array and a simple key function:
myArr = [{'foo': 5, 'bar': 'hello'}, {'foo': 3, 'bar': 'world'}];
keyFunc = obj => obj.foo; // sort by the value of the `foo` attribute
myArr.sort(???);
// expected result: [{'foo': 3, 'bar': 'world'}, {'foo': 5, 'bar': 'hello'}]
Given these constraints, the query remains on how to effectively sort the array?