[JavaScript] Easy to understand - detailed explanation with map or array
Core Strategy\n\nThe purpose of this problem is to sort the array with custom rules. It\'s straightforward, but we need to do more:\n- we need to count the freq
## Core Strategy\n\nThe purpose of this problem is to sort the array with custom rules. It\'s straightforward, but we need to do more:\n- we need to count the frequency first since we don\'t have it\n- we need to do the sorting with frequency and value\n\nFor the first step, we coud use a map or an array to do the counting.\nFor the second step, we could use JS native `sort` method for an array to do custom sorting.\n\n## Using a map\n\nFor this solution, we use a map to do the frequency counting. It\'s straightforward.\n\nHere\'s a sample code from me:\n\n```js\nconst frequencySort = (nums) => {\n  const freq = {};\n  for (const num of nums) {\n    freq[num] = (freq[num] || 0) + 1;\n  }\n  return nums.sort((a, b) => freq[a] === freq[b] ? b - a : freq[a] - freq[b]);\n};\n```\n\n## Using an array and some tricks\n\nFor this solution, since the range of `num[i]` is small, so we could use a fixed-length array to do the counting. Take care of that `OFFSET` since the index of array should be non-negative.\n\nHere\'s a sample code from me:\n\n```js\nconst frequencySort = (nums) => {\n  const freq = new Uint8Array(201);\n  const OFFSET = 100;\n  for (const num of nums) {\n    ++freq[num + OFFSET];\n  }\n  return nums.sort((a, b, a2 = a + OFFSET, b2 = b + OFFSET) => freq[a2] === freq[b2] ? b - a : freq[a2] - freq[b2]);\n};\n```\n
['JavaScript']