实现IComparable的CompareTo接口

SortedSet<T>: 表示按排序顺序保持的对象的集合。

T:必须要实现IComparable接口的CompareTo方法

 

        public static SortedSet<Survey> GetSortedSet()
        {
            SortedSet<Survey> ss = new SortedSet<Survey>();
            ss.Add(new Survey{SurveyID=2,ProvinceId="A1"});
            ss.Add(new Survey{SurveyID=1,ProvinceId="A2"});
            ss.Add(new Survey{SurveyID=5,ProvinceId="A3"});
            return ss;
        }
    public class Survey:IComparable
    {
        public int SurveyID { get; set; }
        public string ProvinceId { get; set; }

        public int CompareTo(object obj)
        {
            if (obj == null)
                return 1;
            Survey survey = obj as Survey;
            if (survey!=null)
            {
                return this.SurveyID.CompareTo(survey.SurveyID);
            }
            else
            {
                throw new ArgumentException("Object is not a Survey");
            }
        }
    }

 

posted @ 2017-05-08 16:07  花生打代码会头痛  阅读(218)  评论(0)    收藏  举报