Imagin I have an array
var arr = [ {name: 'peter', age: 50}, {name: 'alice', age: 50}, {name: 'zebra', age: 50}, ];
Now, I wish to sort them on their name. So, that the output is this:
[{name: 'alice', age: 50}, {name: 'peter', age: 50},{name: 'zebra', age: 50}]
The naive solution is to create an array of names and sort them and then loop through this sorted names, find corresponding item in the arr and insert into the new array.
I know this is not the most optimal algorithm and not the cleanest to write.
Can anyone do it more efficiently? Also, note that I am using JavaScript, so I am limited to the libraries and inbuilt functionalities of javaScript which might be easier if we were using Java or some other similar language I guess.