English 中文(简体)
.net - Array.Sort和Array.BinarySearch的问题 - 文化和全球化
原标题:
  • 时间:2009-04-19 21:38:28
  •  标签:

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 "Y" in most languages is sorted like X-Y-Z while in Lithuanian, the sort order is I-Y-J. This behavior would create havoc to my program.

On a desktop-PC, I could change momentarily the Regional Settings into American English by using:

线程.当前线程.当前文化 = 区域信息.创建指定区域信息("en-US")

然而,由于我正在开发Windows Mobile(CF.NET)版本,所以无法实现此代码。

我找到了一些可以通过编程方式更改设备区域设置的黑客方法,但它们不是“官方”的,并被认为是有风险的,因此我更喜欢避免使用它们。

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?

我相信我可以使用:

Public Shared Function BinarySearch(Of T) ( _
    array As T(), _
    value As T, _
    comparer As IComparer(Of T) _
) As Integer

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.

如果您知道任何在CF.Net中起作用的替代方案,那我当然听候您的指示。

谢谢。

编辑:

我将考虑Twanfosson的答案作为被接受的解决方案,因为我的问题清楚地说明我想保持与英语的联系。

However, in means of flexibility, I believe Guffa s answer is the best one. Why? Let s use another example: In German, the letter Ö is sorted Ö-X-Z while in Swedish and Finnish, the order is X-Z-Ö. In Estonian the sort order is Z-Ö-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 Comparer.DefaultInvariant with its association to English wouldn t help in this case, probably the letter Ö would end up with O. Therefore my vote will go to Guffa.

最佳回答

使用不变文化(Invariant culture)是不可能的吗?

InvariantCulture retrieves an instance of the invariant culture. It is associated with the English language but not with any country/region.

Using the invariant culture would make this trivial.

Array.Sort( myArray, Comparer.DefaultInvariant );

Array.BinarySearch( myArray, myString, Comparer.DefaultInvariant );
问题回答

好的,解决这两个问题的方法是实现一个比较器。创建一个实现IComparer(Of String)接口并拥有自己的CultureInfo对象以用于比较字符串的类:

Public Class StringComparerEnUs
   Implements IComparer(Of String)

   Private _culture As CultureInfo

   Public Sub New()
      _culture = New CultureInfo("en-US")
   End Sub

   Public Function Compare(ByVal x As String, ByVal y As String)
      Return String.Compate(x, y, false, _culture)
   End Function

End Class

Now you can use it to sort the strings:

Array.Sort(theArray, New StringComparerEnUs())

找到它们:

pos = BinarySearch(theArray, "Dent, Arthur", new StringComparerEnUs())

当然,可以通过在构造函数中接受 culture 字符串来使这个类更加通用,你也可以添加一个变量以利用 String.Compare 调用中的 ignoreCase 参数。

请试试看:

class EnglishComparer : IComparer<string>
{
  static CultureInfo __english = new CultureInfo("en-US");

  public int Compare(string x, string y)
  {
    return string.Compare(x, y, __english, CompareOptions.None);
  }
}




相关问题
热门标签