title: .net - Problems with Array.Sort and Array.BinarySearch - culture and globalization
body: <p>I need to sort an array containing a list of words and search the same using binarysearch. For certain reasons, the word-list must always be sorted using the sorting-rules of "en-US" i.e. American Regional Settings. The code will run under various international Operating Systems and of course, this will mean that the word-list will be sorted differently according to the local Regional Settings in use. One problem could arise on a computer/device running with Lithuanian Regional Settings. Why? Because the letter "<strong>Y</strong>" in most languages is sorted like X-<strong>Y</strong>-Z while in Lithuanian, the sort order is I-<strong>Y</strong>-J. This behavior would create havoc to my program.</p>
  
  <p>On a desktop-PC, I could change momentarily the Regional Settings into American English by using:</p>
  
  <p>Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")</p>
  
  <p>However, since I am developing for Windows Mobile (CF.NET), this piece of code is not possible to implement.</p>
  
  <p>I found some hacks which could let me change the Regional Settings on the device programmatically but they are not "official" and considered risky so I prefer avoiding these.</p>
  
  <p>So my question is: how can I force Array.Sort and Array.BinarySearch to use CultureInfo = "en-US" while sorting and searching regardless of the Regional Settings set on the device?</p>
  
  <p>I believe I could use: </p>
  
  <pre><code>Public Shared Function BinarySearch(Of T) ( _
      array As T(), _
      value As T, _
      comparer As IComparer(Of T) _
  ) As Integer
  </code></pre>
  
  <p>and implement Comparer to take into consideration CultureInfo (and set it to "en-US") but I don't know how to do that despite trying hard. If anyone could post some sample-code in VB.Net or C# or an explanation of how to do it, I would be very grateful.</p>
  
  <p>If you are aware of any alternative solution which works in CF.Net, then, of course, I am all ears.</p>
  
  <p>Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>I will consider Twanfosson's answer as the accepted solution since my question clearly stated that I wanted to maintain an association with the English language.</p>
  
  <p>However, in means of flexibility, I believe Guffa's answer is the best one. Why? Let's use another example: In German, the letter <strong>Ö</strong> is sorted <strong>Ö</strong>-X-Z while in Swedish and Finnish, the order is X-Z-<strong>Ö</strong>. In Estonian the sort order is Z-<strong>Ö</strong>-X. Complicated, isn't it? Guffa's solution will let me force Swedish sorting-order (changing CultureInfo) on a device running under German Regional settings. Using <strong>Comparer.DefaultInvariant</strong> with its association to English wouldn't help in this case, probably the letter <strong>Ö</strong> would end up with O. Therefore my vote will go to Guffa.</p>