【发布时间】:2012-11-18 11:42:35
【问题描述】:
是否有一个标准功能可以让我按以下方式对大写和小写字母进行排序,或者我应该实现一个自定义比较器:
student
students
Student
Students
举个例子:
using System;
using System.Collections.Generic;
namespace Dela.Mono.Examples
{
public class HelloWorld
{
public static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("student");
list.Add("students");
list.Add("Student");
list.Add("Students");
list.Sort();
for (int i=0; i < list.Count; i++)
Console.WriteLine(list[i]);
}
}
}
它将字符串排序为:
student
Student
students
Students
如果我尝试使用list.Sort(StringComparer.Ordinal),排序如下:
Student
Students
student
students
【问题讨论】:
-
你需要在这里定制一些东西。
-
你希望结果是什么?
-
@ryadavilli:我希望有一些更懒惰的解决方案! :) 还是谢谢!
-
你只关心第一个字母的大小写吗?如果后面的字母大写怎么办?
标签: c# string sorting case-sensitive