title: Using native Windows natural order sorting in a custom program
body: <p>As a programmer, you have probably had to use or create some kind of string comparison function. Usually, these are pretty simple:</p>
  
  <pre><code>function compare(s1, s1) { return s1.toLowerCase() - s2.toLowerCase(); }
  </code></pre>
  
  <p>This works great for the vast majority of cases. However, Windows <em>(XP and later)</em> sorts files differently -- and better! -- than a poor ASCII implementation.</p>
  
  <p><strong>How can I create a Minimal, Complete, and Verifiable example of native Windows natural order sorting in a custom program?</strong></p>
  
  <p>Everything I have read points to using the <code>StrCmpLogicalW</code> function in <code>shlwapi.dll</code>. That's great! But how can this function be used inside a custom C/C++ program?</p>
  
  <p>I am <strong><em>not</em></strong> interested in re-implementing the compare function. I have already seen <a href="https://stackoverflow.com/a/5641272/2657515">this</a>, <a href="https://stackoverflow.com/a/4165694/2657515">this</a>, <a href="https://stackoverflow.com/q/248603/2657515">this</a>, <a href="https://stackoverflow.com/q/5427506/2657515">this</a>, and <a href="https://stackoverflow.com/q/23818825/2657515">this</a>. These are no doubt very close approximations, but I just want to link or call the Windows API function in my program.</p>
  
  <p>Here are some other things I have researched and tried already:</p>
  
  <ul>
  <li>reading the <a href="https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/nf-shlwapi-strcmplogicalw" rel="nofollow noreferrer">documentation on <code>shlwapi.dll</code> and <code>StrCmpLogicalW</code> from Microsoft</a></li>
  <li>finding a (supposedly) complete program posted from <a href="https://stackoverflow.com/a/5818643/2657515">an earlier Q&amp;A here on StackOverflow</a></li>
  <li>compiling several small code samples for Visual Studio 2010 Express, both C++ and C# versions (<code>fatal error C1190: managed targeted code requires a '/clr' option</code>... <em>really?</em>)</li>
  <li>compiling several small code samples for Visual Studio 2012 Express, because some article said this would get rid of the earlier compile error about the <code>/clr</code> option, but just got a bunch of different compile errors instead</li>
  <li>tried compiling several small code samples for Eclipse C++ with MinGW</li>
  </ul>
  
  <p>When I first started looking into this, I thought, "It's just the Windows API, this will be easy!" I have yet to come up with a working program in any language.</p>
  
  <p>I have been doing C/C++ and Unix/DOS/Windows shell scripting for a long time, and using an API has never been so irksome. Shame on you, Microsoft.</p>
  
  <hr>
  
  <p>Also, I've already read the rants about ASCII sorting, but thank you. These contained some fertile soil for some good thinking.</p>
  
  <p><a href="https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/" rel="nofollow noreferrer">https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/</a></p>
  
  <p><a href="http://weblog.masukomi.org/2007/12/10/alphabetical-asciibetical/" rel="nofollow noreferrer">http://weblog.masukomi.org/2007/12/10/alphabetical-asciibetical/</a></p>