Sort Generic List

后端 未结 3 730
野性不改
野性不改 2021-02-08 09:57

I want to sort a Generic List in Ascending Date order. (Framework v2)

Any suggestions?

3条回答
  •  再見小時候
    2021-02-08 10:32

    Upul and Wim said it all:
    Use delegates:

    new List().Sort(delegate(DateTime d1, DateTime d2) {
        return d1.CompareTo(d2);
    });
    

    Or if you have a class or something that holds that datetime:

    new List().Sort(delegate(Nhonho n1, Nhonho n2) {
        return n1.date.CompareTo(n2.date);
    });
    

    To make it lessening,

    new List().Sort(delegate(Nhonho n1, Nhonho n2) {
        return n2.date.CompareTo(n1.date);
    });
    

    Good luck.

提交回复
热议问题