Easy Python Solution with Explanation
\tfrom collections import Counter\n\tclass Solution:\n\t\tdef frequencySort(self, nums: List[int]) -> List[int]:\n\t\t\td = Counter(nums)\n\t\t\tdef check(x):\n
\tfrom collections import Counter\n\tclass Solution:\n\t\tdef frequencySort(self, nums: List[int]) -> List[int]:\n\t\t\td = Counter(nums)\n\t\t\tdef check(x):\n\t\t\t\treturn d[x]\n\n\t\t\tnums.sort(reverse=True)\n\t\t\tnums.sort(key=check)\n\t\t\treturn nums\n\t\t\t\nSo the basic idea is we use the Python\'s inbuilt sort function with a custom method which returns the frequency of that element in the array to sort. So, first we create a frequency array using the Python\'s Counter function *(This returns a dictionary, the same could have been manually using a blank dictionary but since we can utilise the capabilities of Python, that is what we do here.)* \n\nand we create a new function which returns the frequency of that element in our array *(The same could have been done easier with a lambda function as well, but this was my first instinct so I went with this)*\n\nNow, we reversed sorted the list first normally so that when sorting the second time using our custom sort, our answer comes out to be what we want, *(decreasing order for elements with the same frequency)* \n\nNext we use our custom sort function which generates a frequency wise + decreasing order for same frequency elements. Our inbuilt custom sort is stable, meaning it won\'t change the relative order among elements. \n\nExample:\n\tIn Stable Frequency Wise Sort:\n\t`[1,3,3,2,4] --> [1,2,4,3,3] (relative order of 1,2,4 is maintained)`\n\tIn Unstable Frequency Wise Sort:\n\t`[1,3,3,2,4] --> [4,1,2,3,3] (relative order of 1,2,4 is not maintained)`\n\t\n\n**Note:** We can achieve this in one sort also by instead of returning a single key in our *check* function, we return a tuple of keys. \n\nSo that would make our *check* function something like: \n\n```\ndef check(x):\n\treturn (d[x], -x)\n```\nThe purpose of *-x* is because in case of elements with the same frequency, we want them sorted in reverse sorted order.\n\nExample: `nums = [1,2,3,4]`\n\nSo, for this list, the element:\n* 1 --> (1, -1)\n* 2 --> (1, -2)\n* 3 --> (1, -3)\n* 4 --> (1, -4)\n* element --> (frequency of element, -element)\n\nSo, first our list is sorted based on the first element, so since all are 1. Nothing happens. Next, we sort all the 1s based on the second element. So, -4 is the smallest so, that comes first, then -3 then -2 and lastly -1.\n\nSo the order becomes: (1, -4) , (1, -3), (1, -2), (1, -1) which means [4,3,2,1]. Which is what we wanted. \n\nPlease feel free to ask anything here, I will be happy to help/respond.
['Sorting', 'Python', 'Python3']