Python solution
Code\n\nfrom collections import Counter\n\nclass Solution:\n    def frequencySort(self, nums: List[int]) -> List[int]:\n        freq = Counter(nums)\n        nu
# Code\n```\nfrom collections import Counter\n\nclass Solution:\n    def frequencySort(self, nums: List[int]) -> List[int]:\n        freq = Counter(nums)\n        nums.sort(key=lambda x: (freq[x], -x))\n        return nums\n\n```\n### Example 1\n**Input:** `nums = [1, 1, 2, 2, 2, 3]`\n\n**Step 1: Count the frequency of each element**\n\nWe use the `Counter` from the `collections` module to count the frequency of each element in the array.\n\n```python\nfrom collections import Counter\n\nnums = [1, 1, 2, 2, 2, 3]\nfreq = Counter(nums)\nprint(freq)\n```\n\nThe output will be:\n```\nCounter({2: 3, 1: 2, 3: 1})\n```\n\nThis means:\n- The number `2` appears 3 times.\n- The number `1` appears 2 times.\n- The number `3` appears 1 time.\n\n**Step 2: Sort the array based on the frequency and value**\n\nWe need to sort the array such that:\n1. Elements with lower frequency come first.\n2. If two elements have the same frequency, the one with the higher value comes first.\n\nThe sorting key we use is `(freq[x], -x)`, where:\n- `freq[x]` ensures elements are sorted by their frequency in increasing order.\n- `-x` ensures that if frequencies are the same, elements are sorted by their value in decreasing order.\n\n```python\nnums.sort(key=lambda x: (freq[x], -x))\nprint(nums)\n```\n\nThe output will be:\n```\n[3, 1, 1, 2, 2, 2]\n```\n\n**Explanation:**\n- `3` has a frequency of 1, so it comes first.\n- `1` has a frequency of 2, so it comes next.\n- `2` has a frequency of 3, so it comes last.\n\n### Example 2\n**Input:** `nums = [2, 3, 1, 3, 2]`\n\n**Step 1: Count the frequency of each element**\n\n```python\nnums = [2, 3, 1, 3, 2]\nfreq = Counter(nums)\nprint(freq)\n```\n\nThe output will be:\n```\nCounter({2: 2, 3: 2, 1: 1})\n```\n\nThis means:\n- The number `2` appears 2 times.\n- The number `3` appears 2 times.\n- The number `1` appears 1 time.\n\n**Step 2: Sort the array based on the frequency and value**\n\n```python\nnums.sort(key=lambda x: (freq[x], -x))\nprint(nums)\n```\n\nThe output will be:\n```\n[1, 3, 3, 2, 2]\n```\n\n**Explanation:**\n- `1` has a frequency of 1, so it comes first.\n- `3` and `2` both have a frequency of 2. Since `3` is greater than `2`, `3` comes before `2`.\n\n### Example 3\n**Input:** `nums = [-1, 1, -6, 4, 5, -6, 1, 4, 1]`\n\n**Step 1: Count the frequency of each element**\n\n```python\nnums = [-1, 1, -6, 4, 5, -6, 1, 4, 1]\nfreq = Counter(nums)\nprint(freq)\n```\n\nThe output will be:\n```\nCounter({1: 3, -6: 2, 4: 2, -1: 1, 5: 1})\n```\n\nThis means:\n- The number `1` appears 3 times.\n- The number `-6` appears 2 times.\n- The number `4` appears 2 times.\n- The number `-1` appears 1 time.\n- The number `5` appears 1 time.\n\n**Step 2: Sort the array based on the frequency and value**\n\n```python\nnums.sort(key=lambda x: (freq[x], -x))\nprint(nums)\n```\n\nThe output will be:\n```\n[5, -1, 4, 4, -6, -6, 1, 1, 1]\n```\n\n**Explanation:**\n- `5` and `-1` both have a frequency of 1. Since `5` is greater than `-1`, `5` comes first.\n- `4` and `-6` both have a frequency of 2. Since `4` is greater than `-6`, `4` comes before `-6`.\n- `1` has the highest frequency of 3, so it comes last.\n\n
['Python3']