My function below returns an object containing the sorted playerPoints
passed in via the "array of objects" parameter. Without modifying the passed in array, is there a more efficient/performant way to add elements and sort the array in ascending order?
Note: The array of objects could contain one or more objects. In terms of efficiency, I'm referring to completion time (speed). Also the function, ascendingSort is required and can't be replaced.
var data = [{playerPoints: 10}, {playerPoints: 8}, {playerPoints: 2}, {playerPoints: 21},{playerPoints: 30}];
function ascendingSort(players) {
var points = [];
for(var i = 0; i < players.length; i++) {
points.push(players[i].playerPoints);
}
points.sort(function(a, b) {return a - b;});
return {sortedPoints: points};
}
var obj = ascendingSort(data);
console.log(obj);