So here is what I got: I have a list with these strings in it: "student", "Students", "students", "Student" and "_Students".
What I have done:
List<string> sort = new List<string>() { "student", "Students", "students", "Student","_Students" };
List<string> custsort = sort.OrderBy(st => st[0]).ThenBy(s => s.Length)
.ToList();
But this gives me this sort:
Student
Students
_Students
student
students
And what I want is:
_Students
Student
Students
student
students
I can't figure it out how to sort them because that damn underscore is located between the upperCase and lowerCase letters in the ASCII table.
You can use a regex to determine whether the first character is a letter and apply a weight to it.
int GetWeight(char c)
{
return Regex.IsMatch(c.ToString(), @"[a-zA-Z]") ? c : 0;
}
List<string> sort = new List<string>() { "student", "Students", "students", "Student","_Students" };
List<string> custsort =
sort.OrderBy(st => GetWeight(st[0]))
.ThenBy(s => s.Length)
.ToList();
This way if you need any other special rules you can modify the GetWeight
function and your Linq will be unaffected.