在c#中按字母顺序对单词数组进行排序,然后从大写到小写

问题描述 投票:0回答:1

我有一个数组中的单词输入,我想按字母顺序排序,然后从大写到小写。我使用 Linq 来做到这一点。如果我给出这样的输入 - {CBA abc aBc ABC cba CBA},那么我希望排序的输出像这样 {ABC aBc abc CBA CBA cba}

我的程序是这样的 -

public static string Inputsorter(string someInput)
{
    string[] words = someInput.Split(' ');
    words = words
        .OrderBy(x => x, StringComparer.OrdinalIgnoreCase)
        .ThenBy(x => x,  StringComparer.Ordinal)
        .ToArray();

    someInput = string.Join(' ', words);
    return someInput;            
}
c# arrays linq sorting textinput
1个回答
0
投票

这个简单的测试控制台应用程序:

string[] words = [ "CBA", "abc", "aBc", "ABC", "cba", "CBA" ];
Array.Sort(words);
foreach (string s in words) {
    Console.WriteLine(s);
}
Console.ReadKey();

打印:

abc
aBc
ABC
cba
CBA
CBA
© www.soinside.com 2019 - 2024. All rights reserved.