C# list 多条件排序
作者:互联网
private int SortByID(HorseDate a1, HorseDate a2) { if (a1.level.CompareTo(a2.level) != 0) return -(a1.level.CompareTo(a2.level)); else if (a1.ToLevel.CompareTo(a2.ToLevel) != 0) return -(a1.ToLevel.CompareTo(a2.ToLevel)); else if (a1.Quality.CompareTo(a2.Quality) != 0) return -(a1.Quality.CompareTo(a2.Quality)); else if (a1.ID.CompareTo(a2.ID) != 0) return -(a1.ID.CompareTo(a2.ID)); else return 1; }
程序中直接调用
horseList,.Sort(SortByID);
最后,有人注意到为何return返回的降序吗?哈哈,由于前面提到过这个功能是要按照降序排列的,因此前面须要加负号,若是是升序排列就不须要了
标签:return,C#,ToLevel,list,else,a1,a2,CompareTo,排序 来源: https://www.cnblogs.com/feifeifeisir/p/16358633.html