C# – Sorting a List of Objects

// Use IComparable Interface and implement CompareTo method. Uses the standard -1, 0, 1

class MyObject: IComparable
    public string Label;

    public int CompareTo(object obj)
        if (obj == null) return 1;
        MyObject otherObject = obj as MyObject;
        if (otherObject != null)
            return this.Label.CompareTo(otherObject.Label);
            throw new ArgumentException("Object is not a MyObject");

List myObjects = createMyObjects(); // defined elsewhere

One thought on “C# – Sorting a List of Objects

  1. Pingback: ルイヴィトン lvmh

Comments are closed.