我对字符串进行排序的代码:字典顺序排序C#
class Program
{
static void Main()
{
int x = Convert.ToInt32(Console.ReadLine());
List<string> sampleList = new List<string>();
for (int i=0; i<x; i++)
{
string word = Console.ReadLine();
sampleList.Add(word);
}
foreach (string s in SortByLength(sampleList))
{
Console.Write(s);
}
Console.ReadLine();
}
static IEnumerable<string> SortByLength(IEnumerable<string> e)
{
// Use LINQ to sort the array received and return a copy.
var sorted = from s in e
orderby s.Length descending
select s;
return sorted;
}
}
此代码排序的长字符串,我怎么能做到这一点的长度和字典顺序?
例
//Input
4
abba
abacaba
bcd
er
//Output
abacabaabbabcder
在这种情况下,做工精细,但当我有
//Input
5
abba
ebacaba
bcd
er
abacaba
//Output
ebacabaabacabaabbabcder
我的第一个字符串是ebacaba这是不对的。
你可以试试: 'VAR分类= e.OrderByDescending(X => X。长度).ThenBy(x => x).ToList()' –
我不明白你想为第二种情况(5输入)实现的模式,介意给予预期的输出? – Ian