7

I have a webpage, in which there is a sort that I have to order the list by Chinese strokes.

I have created an application containing code like this:

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();

but there is an error: At least one object must implement IComparable.

What does this mean and how can I fix it?

ToddZhao
  • 73
  • 5

4 Answers4

7

You need to have your Student class implement the IComparable interface. This requires implementing a method CompareTo, which can simply return the result of calling CompareTo between the strings you're trying to order by.

For example, if the constructor initializes a name field, you might have something like this:

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);
    }
}
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
3

Student must implement IComparable.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
3

Rather than implement IComparable, why not just use a bit of LINQ?

stuList.OrderBy( s => s.Name ) //.ToList if you really want a List
spender
  • 117,338
  • 33
  • 229
  • 351
  • if i use this 'orderby' ,the result is not quite right . else ,if the COLLATE of table is 'SQL_Latin1_General_CP1_CI_AS',it's not what i want . i must order the list with 'Chinese_PRC_Stroke_ci_as'. my e is very poor , there are some mistakes ,please ! – ToddZhao Nov 21 '11 at 03:10
0
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
}
  • Why are you returning MinValue if obj is null? That seems kind of weird. – dan-gph Nov 21 '11 at 02:47
  • @Dangph: It simply means that all Students should compare before `null`, which is not unreasonable. – Platinum Azure Nov 21 '11 at 04:21
  • @Platinum Azure, the docs for IComparable say, "By definition, any object compares greater than null". So it's the wrong way round. Also, why MinValue in particular? If I saw that I would assume it had some special meaning, but it doesn't. – dan-gph Nov 21 '11 at 04:38
  • @Dangph: Isn't that just a convention? – Platinum Azure Nov 21 '11 at 15:15
  • @Platinum Azure, if you are referring to objects comparing greater than null, I would say that is part of the *specification* for IComparable, it's not just a convention. If you are referring to MinValue, I would say that the convention for a default negative number is to use -1. – dan-gph Nov 22 '11 at 05:18
  • @Dangph Thanks Dangph, you are right. I didn't think the given example too much, just want to return a negative number. –  Nov 22 '11 at 05:46