我剛剛做了什麼可能是(有限)解決您的問題。 這不是最優化的,但它可以給出如何解決它的想法。 我創建了一個LithuanianString
類,它僅用於封裝您的字符串。 此類實現IComparable
以便能夠對LithuanianString
的列表進行排序。
以下是可能是一流的:
public class LithuanianString : IComparable<LithuanianString>
{
const string UpperAlphabet = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ";
const string LowerAlphabet = "aąbcčdeęėfghiįyjklmnoprsštuųūvzž";
public string String;
public LithuanianString(string inputString)
{
this.String = inputString;
}
public int CompareTo(LithuanianString other)
{
var maxIndex = this.String.Length <= other.String.Length ? this.String.Length : other.String.Length;
for (var i = 0; i < maxIndex; i++)
{
//We make the method non case sensitive
var indexOfThis = LowerAlphabet.Contains(this.String[i])
? LowerAlphabet.IndexOf(this.String[i])
: UpperAlphabet.IndexOf(this.String[i]);
var indexOfOther = LowerAlphabet.Contains(other.String[i])
? LowerAlphabet.IndexOf(other.String[i])
: UpperAlphabet.IndexOf(other.String[i]);
if (indexOfOther != indexOfThis)
return indexOfThis - indexOfOther;
}
return this.String.Length - other.String.Length;
}
}
這裏是我做了嘗試它的樣本:
static void Main(string[] args)
{
string[] unordered = { "Za", "Žb", "Ūa", "Ub" };
//Create a list of lithuanian string from your array
var lithuanianStringList = (from unorderedString in unordered
select new LithuanianString(unorderedString)).ToList();
//Sort it
lithuanianStringList.Sort();
//Display it
Console.WriteLine(Environment.NewLine + "My Comparison");
lithuanianStringList.ForEach(c => Console.WriteLine(c.String));
}
輸出是預期之一:
UbŪaZaŽb
該類僅允許通過替換開頭定義的兩個常量中的字符來創建自定義字母。
http://stackoverflow.com/questions/1371813/why-does-string-compare-seem-to-handle-accented-characters-inconsistently –