c#列表<;T>;排序:按中文笔画

c#列表<;T>;排序:按中文笔画,c#,list,stroke,C#,List,Stroke,我有一个网页,其中有一个排序,我必须按中文笔画排序 我创建了一个包含以下代码的应用程序: List<Student> stuList = new List<Student>() { new Student("上海"), new Student("深圳"), new Student("广州"), new Student("香港") }; Sy

我有一个网页,其中有一个排序,我必须按中文笔画排序

我创建了一个包含以下代码的应用程序:

List<Student> stuList = new List<Student>() { 
          new Student("上海"),
           new Student("深圳"),
            new Student("广州"),
             new Student("香港")
            };
        System.Globalization.CultureInfo strokCi = new System.Globalization.CultureInfo("zh-tw");
        System.Threading.Thread.CurrentThread.CurrentCulture = strokCi; ;
        //stuList.sort();
public class Student : IComparable
{
    private string name;

    public Student(string name)
    {
        this.name = name;
    }

    public int CompareTo(object other)
    {
        Student s = other as Student;
        if (s == null)
        {
            throw new ArgumentException("Students can only compare with other Students");
        }

        return this.name.CompareTo(s.name);
    }
}
List stuList=new List(){
新生(“上海"),
新生(“深圳"),
新生(“广州"),
新生(“香港")
};
System.Globalization.CultureInfo strokCi=新的System.Globalization.CultureInfo(“zh-tw”);
System.Threading.Thread.CurrentThread.CurrentCulture=strokCi;
//stuList.sort();
但是有一个错误:
至少有一个对象必须实现IComparable。


这意味着什么?我如何修复它?

Student
必须实现
IComparable
您需要让您的
Student
类实现
IComparable
接口。这需要实现一个方法
CompareTo
,它可以简单地返回调用
CompareTo
b的结果在您试图订购的字符串之间

public class Student : IComparable
{
    private string message = null;
    public Student(string message)
    {
        this.message = message;
    }
    #region IComparable Members

    public int CompareTo(object obj)
    {
        // implement your logic, here is a example:
        if (obj != null)
            return message.CompareTo(((Student)obj).message);
        return int.MinValue;
    }

    #endregion
}
例如,如果构造函数初始化一个
名称
字段,您可能会遇到如下情况:

List<Student> stuList = new List<Student>() { 
          new Student("上海"),
           new Student("深圳"),
            new Student("广州"),
             new Student("香港")
            };
        System.Globalization.CultureInfo strokCi = new System.Globalization.CultureInfo("zh-tw");
        System.Threading.Thread.CurrentThread.CurrentCulture = strokCi; ;
        //stuList.sort();
public class Student : IComparable
{
    private string name;

    public Student(string name)
    {
        this.name = name;
    }

    public int CompareTo(object other)
    {
        Student s = other as Student;
        if (s == null)
        {
            throw new ArgumentException("Students can only compare with other Students");
        }

        return this.name.CompareTo(s.name);
    }
}

与其实现
IComparable
,为什么不使用一点LINQ呢

stuList.OrderBy( s => s.Name ) //.ToList if you really want a List

如果obj为null,为什么要返回MinValue?这似乎有点奇怪。@Dangph:这只是意味着所有学生都应该在
null
之前进行比较,这并不是不合理的。@Platinum Azure,IComparable的文档说,“根据定义,任何对象的比较都大于null"。所以这是错误的。还有,为什么特别是MinValue?如果我看到了,我会认为它有一些特殊的含义,但它没有。@Dangph:这不只是一个约定吗?@Platinum Azure,如果你指的是比较大于null的对象,我会说这是IComparable规范的一部分,它不仅仅是一个约定。如果你指的是MinValue,我会说默认负数的惯例是使用-1。如果我使用这个“orderby”,结果就不太正确。否则,如果表的排序是“SQL\u Latin1\u General\u CP1\u CI\u AS”,这不是我想要的。我必须用“Chinese\u PRC\u Stroke\u CI\u AS”来排序列表。我的e很差,有一些错误请