小编Art*_*aca的帖子

按字典顺序排序 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

该代码按长度对字符串进行排序,我该如何按长度和字典顺序进行排序?

例子

//Input
4
abba
abacaba
bcd
er …
Run Code Online (Sandbox Code Playgroud)

c# sorting lexicographic

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

lexicographic ×1

sorting ×1