Top Score Sorter

const maxScore = 1218000;
const scoreSet = [874300, 879200, 1172100, 1141800, 933900, 1177200, 1190200, 1110100, 1158400, 985600, 1047200, 1049100, 1138600, 1170500, 1064500, 1190000, 1050200, 1090400, 1062800, 1061700, 1218000, 1068000, 1127700, 1144800, 1195100];

function scoreSettler(scores, max) {
  "use strict";
  
  // It can't be as simple as just calling Array.protoype.sort(), can it?
  // The below is about as short and concise as can be. Array.sort compares
  // string versions of the data to sort so it can provide incorrect results
  // on numeric data sets. In this case we provide a function to sort the numbers
  // in descending order.
  
  //return scores.sort((a, b) => b - a);
  
  // This is beautiful but the algorithm that is used cannot be any faster than
  // O(n log n), apparently. Lets see if we can use the max score possible to
  // speed this up.
  
  // We can use the max score to sort the values by finding their difference.
  // This will create a very sparse array that we can then filter out the empty
  // indexes from.
  
  const sparseArr = [];
  for (let i = 0, l = scores.length; i < l; i++) {
    let diff = max - scores[i];
    sparseArr[diff] = scores[i];
  }
  
  return sparseArr.filter(ele => ele);
  // This is still linear becasue filter is just checking for truthiness and
  // not employing any crazy long processes.
}

console.log(scoreSettler(scoreSet, maxScore));