title: A more elegant way to cluster sorted characters together?
body: <p>Suppose, I want to sort characters in a string by their frequency.</p>
  <pre><code>def frequencySort(self, s: str) -&gt; str:
      
      freq = {}
      
      for i in s:
          if i in freq:
              freq[i] += 1
          else:
              freq[i] = 1
      
      print(freq)
      
      sorted_chars = sorted(s, key = lambda x : -freq[x])
      
      return &quot;&quot;.join(sorted_chars)
  </code></pre>
  <p>However, for inputs where some characters have equal frequency, these characters end up intermingled together in the output.</p>
  <p>E.g., for input <code>loveleetcode</code> the code above outputs <code>eeeelolovtcd</code> whereas it should output <code>eeeeoollvtcd</code> or <code>eeeelloovtcd</code> (o's and l's should be clustered together, not intermixed).</p>
  <p>So, I add a tie breaker that uses a fraction of the character's ASCII code (end's up being less than one, so guaranteed not to overpower next most frequent char):</p>
  <pre><code>def frequencySort(self, s: str) -&gt; str:
      
      freq = {}
      
      for i in s:
          if i in freq:
              freq[i] += 1
          else:
              freq[i] = 1
      
      # Tie-breaker:
      
      for i in freq:
          freq[i] += ord(i) / 1000
      
      sorted_chars = sorted(s, key = lambda x : -freq[x])
      
      return &quot;&quot;.join(sorted_chars)
  </code></pre>
  <p>This works fine, but I was wondering if there is a more elegant (pythonic) way to enforce clustering of similar characters during the sort.</p>