2016-09-21 63 views
0

我需要從文件中將字母排序爲字母。我怎樣才能做到這一點?我需要ToString方法。現在控制檯打印:C#排序立陶宛字母

ABCDEFGIJKLM ... ... ACEI

我需要得到這個:

AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ

感謝

static char[] Letters(string e) // 
    { 
     e = e.ToUpper(); 

     char[] mas = new char[32]; 
     int n = 0; 
     foreach (char r in e) 
      if (Array.IndexOf(mas, r) < 0) 
       if (Char.IsLetter(r)) 
        mas[n++] = r; 
     Array.Resize(ref mas, n); 
     Array.Sort(mas); 
     return mas; 
    } 
+1

[查看Jeppe Stig Nielsen的評論](http://stackoverflow.com/a/34489023/3060520) –

回答

0

您可以通過使用能理解一個比較器如何比較排序字符解決這個問題字母按字母順序排列(默認爲序號比較)。

這個實現是非常低效的,因爲它轉換字符爲字符串每次做一個比較的時間,但它的工作原理:

public class CharComparer : IComparer<char> 
{ 
    readonly CultureInfo culture; 

    public CharComparer(CultureInfo culture) 
    { 
     this.culture = culture; 
    } 

    public int Compare(char x, char y) 
    { 
     return string.Compare(new string(x, 1), 0, new string(y, 1), 0, 1, false, culture); 
    } 
} 

(注:文化是沒有實際必要在這裏;它的工作原理沒有它。我只是包括它的完整性)

然後你就可以使用與接受IComparer排序功能,如Array.Sort()

static void Main() 
{ 
    var test = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ".ToCharArray(); 
    Console.OutputEncoding = System.Text.Encoding.Unicode; 

    Array.Sort(test); 
    Console.WriteLine(new string(test)); // Wrong result using default char comparer. 

    Array.Sort(test, new CharComparer(CultureInfo.GetCultureInfo("lt"))); // Right result using string comparer. 
    Console.WriteLine(new string(test)); 
} 

另一種方法是使用單字符字符串數組而不是字符數組,然後對其進行排序。這工作,因爲排序功能將使用字符串比較器,其理解字母順序:

var test = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ".Select(x => new string(x, 1)).ToArray(); 
Console.OutputEncoding = System.Text.Encoding.Unicode; 

Array.Sort(test); // Correct result because it uses the string comparer, which understands alphabetical order. 
Console.WriteLine(string.Concat(test)); 

或者使用LINQ:

var test = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ".Select(x => new string(x, 1)).ToArray(); 
Console.OutputEncoding = System.Text.Encoding.Unicode; 

// Correct result because it uses the string comparer, which understands alphabetical order. 
test = test.OrderBy(x => x).ToArray(); 
Console.WriteLine(string.Concat(test)); 

使用字符串代替字符數組的數組可能是更好的性能當這樣排序時。

0

你可以使用following method刪除diacritics

static string RemoveDiacritics(string text) 
{ 
    var normalizedString = text.Normalize(NormalizationForm.FormD); 
    var stringBuilder = new StringBuilder(); 

    foreach (var c in normalizedString) 
    { 
     var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); 
     if (unicodeCategory != UnicodeCategory.NonSpacingMark) 
     { 
      stringBuilder.Append(c); 
     } 
    } 

    return stringBuilder.ToString().Normalize(NormalizationForm.FormC); 
} 

然後你可以使用這些字符的順序:

string e = "ABCDEFGIJKLM...ĄČĖĮ..."; 
var normalizedCharList = e.Zip(RemoveDiacritics(e), (chr, n) => new { chr, normValue = (int)n }).ToList(); 
var orderedChars = normalizedCharList.OrderBy(x => x.normValue).Select(x => x.chr); 
string ordered = new String(orderedChars.ToArray());