2

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);

2 Answers2

6

To simplify this function, you can use the map() method to map objects to playerPoints property. Also, you can change the regular function passed to the sort() method (function(a, b) {return a - b;}) to an arrow function ((a, b) => a - b).

const data = [{playerPoints: 10}, {playerPoints: 8}, {playerPoints: 2}, {playerPoints: 21},{playerPoints: 30}]

function ascendingSort(players) {
  return {
    sortedPoints: data.map(x => x.playerPoints).sort((a, b) => a - b)
  }
}

const obj = ascendingSort(data)
console.log(obj)

As for performance, I doubt there's a faster solution than that one you already have.

Michał Perłakowski
  • 70,955
  • 24
  • 137
  • 155
  • They can, but is it more efficient? The question isn't just asking for a different approach. What do the benchmarks show? – Quentin Sep 01 '16 at 09:57
  • thanks for your lambda expression look a like method. Often see `map` method, but never see the implementation, up-voted :) – Fadhly Permata Sep 01 '16 at 09:58
  • "you can change the regular function passed to the sort() method (function(a, b) {return a - b;}) to an arrow function " — Again, they can, but why should they? The primary reason for using an arrow function is that it affects the value of `this` … which isn't used in the function. – Quentin Sep 01 '16 at 09:59
  • @Quentin I think that by "efficient" the OP meant "simpler", not "faster". Unless they're processing a lot of data, speed doesn't matter that much. – Michał Perłakowski Sep 01 '16 at 10:00
  • @Quentin "The primary reason for using an arrow function is that it affects the value of `this`" Not really, see [When should I use Arrow functions in ECMAScript 6?](http://stackoverflow.com/q/22939130/3853934) – Michał Perłakowski Sep 01 '16 at 10:02
  • I edited my question to be more clearer. I have to use the function, ascendingSort to contain my logic and return an object containing the sorted points array. – redgroove16 Sep 01 '16 at 10:18
0

Can you just replace the function with

  function ascendingSort(players) {
      points.sort(function(a, b){return a["playerPoints"] - b["playerPoints"]});
      return {sortedPoints: players};
}
CBusBus
  • 2,219
  • 16
  • 24