C# sorting strings small and capital letters

青春壹個敷衍的年華 提交于 2019-12-01 19:00:50
V4Vendetta

Do you mean something on these lines

List<string> sort = new List<string>() { "student", "Students", "students", 
                                         "Student" };
List<string> custsort=sort.OrderByDescending(st => st[0]).ThenBy(s => s.Length)
                                                         .ToList();

The first one orders it by the first character and then by the length. It matches the output you suggested by then as per the pattern i mentioned above else you will do some custom comparer

I believe you want to group those strings which starts with lower case and upper case, then sort them separately.

You can do:

list = list.Where(r => char.IsLower(r[0])).OrderBy(r => r)
      .Concat(list.Where(r => char.IsUpper(r[0])).OrderBy(r => r)).ToList();

First select those string which starts with lower case, sort them, then concatenate it with those strings which start with upper case(sort them). So your code will be:

List<string> list = new List<string>();
list.Add("student");
list.Add("students");
list.Add("Student");
list.Add("Students");
list = list.Where(r => char.IsLower(r[0])).OrderBy(r => r)
      .Concat(list.Where(r => char.IsUpper(r[0])).OrderBy(r => r)).ToList();
for (int i = 0; i < list.Count; i++)
    Console.WriteLine(list[i]);

and output:

student
students
Student
Students
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!