2016-09-21 52 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());