title: Sorting a dictionary by key in VBA
body: <p>I have created a dictionary in VBA using <code>CreateObject("Scripting.Dictionary")</code> that maps source words to target words to be replaced in some text (This is actually for obfuscation).</p>
  
  <p>Unfortunately, when I do the actual replace as per the code below, it will replace the source words in the order they were added to the dictionary. If I then have for instance "Blue" and then "Blue Berry", the "Blue" part in "Blue Berry" is replaced by the first target and " Berry" remains as it was.</p>
  
  <pre><code>'This is where I replace the values
  For Each curKey In dctRepl.keys()
      largeTxt = Replace(largeTxt, curKey, dctRepl(curKey))
  Next
  </code></pre>
  
  <p>I'm thinking that I could resolve this issue by first sorting the dictionary's <strong>keys</strong> from longest length to shortest length and then doing the replace as above. The problem is I don't know how to sort the keys this way.</p>