Python 3 solution - with process of thinking and improvement
Part 1\n\nSunday morning, Spotify, coffee, console... Task from the list of tasks to solve later.\nLet\'s go!\n\npython\n~ \u276F python3                       
# Part 1\n\nSunday morning, Spotify, coffee, console... Task from the list of tasks to solve later.\nLet\'s go!\n\n```python\n~ \u276F python3                                                                                                                     at 10:38:03\nPython 3.9.1 (default, Feb  3 2021, 07:38:02)\n[Clang 12.0.0 (clang-1200.0.32.29)] on darwin\nType "help", "copyright", "credits" or "license" for more information.\n>>> nums = [1,1,2,2,2,6,6,4,4,5,5,3]\n```\n\nOk, so I need to count frequency of each of the unique values... First idea - use `Counter`. This returns `collection` type of object:\n\n```python\n>>> d = Counter(nums)\n>>> d\nCounter({2: 3, 1: 2, 6: 2, 4: 2, 5: 2, 3: 1})\n>>> type(d)\n<class \'collections.Counter\'>\n```\n\nNow I need to sort those values following the requirements in the description. \'Counter\' object has no attribute \'sort\', and \'sorted\' will only sort values, not frequencies. Googling options, found this [StackOverflow question](https://stackoverflow.com/q/20950650/4440387) with lots of useful answers. Reading... Let\'s try easier object:\n\n```python\n>>> r = Counter(nums).most_common()\n```\n\nThis returns a list of tuples, where first number is value and second - its\' frequency. Can I sort it in the same command? Jumping to the [official documentation](https://docs.python.org/3/library/collections.html#collections.Counter.most_common). Nope, not sortable in the command itself, moreover: "*Elements with equal counts are ordered in the order first encountered*". Ok, let\'s sort it directly, first by values in the decreasing order, then by frequencies in the increasing.\n\n```\n>>> r.sort(key = lambda x: x[0], reverse=True)\n>>> r.sort(key = lambda x: x[1])\n>>> r\n[(3, 1), (6, 2), (5, 2), (4, 2), (1, 2), (2, 3)]\n```\n\nLooks promising. Now I want to expand those tuples into a single list... Still browsing answers to the same [question](https://stackoverflow.com/q/20950650/4440387). Remembering that I can expand tuple and get every number from it by using this:\n\n```\n>>> a, b = (3, 2)\n>>> a\n3\n>>> b\n2\n```\n\nso then I can repeat every value by the number of its\' frequency like so:\n\n```\n>>> [3]*2\n[3, 3]\n```\n\nAha. Now I need an empty list to combine all those tuples into a single list:\n\n```\nt = []\nfor i in r:\n    a, b = i\n    t.extend([a] * b)\n\n>>> t\n[3, 6, 6, 5, 5, 4, 4, 1, 1, 2, 2, 2]\n```\n\nWoo-hoo! That\'s what I need. So the complete solution now looks like this:\n\n```\nclass Solution:\n    def frequencySort(self, nums: List[int]) -> List[int]:\n        \n        r = Counter(nums).most_common()\n        r.sort(key = lambda x: x[0], reverse=True)\n        r.sort(key = lambda x: x[1])\n        \n        t = []\n        for i in r:\n            a, b = i\n            t.extend([a]*b)\n            \n        return t\n```\n\n**Result**:\nRuntime: 52 ms, faster than 63.30% of Python3 online submissions for Sort Array by Increasing Frequency.\nMemory Usage: 14.2 MB, less than 58.20% of Python3 online submissions for Sort Array by Increasing Frequency.\n\nNot the best, but the task is solved.\n\n# Part 2\n\nNow it\'s time for another *fun* - can I make it one-liner or optimize the solution in any other way?\n\nLooking at sorting lines... Can I sort it in one go? Yes! So, first we sort by values in the reverse order (`-x[0]`) and then by their frequencies (`x[1]`) in direct order. \n```\n>>> r.sort(key = lambda x: (x[1], -x[0]))\n```\n\nBasically, it\'s the same operation as the above but now coded in one line. Love Python :) Same logic applies to the tuple expansion part and it allows to save another line:\n\n```\nt = []\nfor i in r:\n\tt += ([i[0]] * i[1])\n```\n\nAnd then I thought - if I can sort by value and its\' frequency why do I need intermediate list? Can I sort the original list the same way?! Let\'s see...\n\n```python\n>>> nums\n[1, 1, 2, 2, 2, 6, 6, 4, 4, 5, 5, 3]\n>>> r = Counter(nums)\n>>> r\nCounter({2: 3, 1: 2, 6: 2, 4: 2, 5: 2, 3: 1})\n>>> nums.sort(key=lambda x: (r[x], -x))\n>>> nums\n[3, 6, 6, 5, 5, 4, 4, 1, 1, 2, 2, 2]\n```\n\nVoila! That feels sooo good. But `x.sort` makes it in-place and I need to return an object... So, I need to change it to `sorted` then:\n\n```\n>>> result = sorted(nums, key=lambda x: (r[x], -x))\n>>> result\n[3, 6, 6, 5, 5, 4, 4, 1, 1, 2, 2, 2]\n```\n\nPerfect. So the final variant would be:\n```python\nclass Solution:\n    def frequencySort(self, nums: List[int]) -> List[int]:\n        r = Counter(nums)\n        return sorted(nums, key=lambda x: (r[x], -x))\n```\n\nAnd it\'s even faster!\n\nRuntime: 44 ms, faster than 95.07% of Python3 online submissions for Sort Array by Increasing Frequency.\nMemory Usage: 14.3 MB, less than 58.20% of Python3 online submissions for Sort Array by Increasing Frequency.\n\n**NOTE**\n* If you want to downvote this post, please, be brave and comment why. This will help me and others to learn from it.\n* If you think that others can learn something from this post, then please, upvote it. Thanks.
['Python', 'Python3']