zoukankan      html  css  js  c++  java
  • c# List排序

    list.Sort((s1, s2)=> s1.CompareTo(s2));

    5.CompareTo(6) = -1 First int is smaller. 6.CompareTo(5) = 1 First int is larger. 5.CompareTo(5) = 0 Ints are equal.


    orderList.Sort(delegate(Order p1,Order p2){int compareDate = p1.Date.CompareTo(p2.Date);if(compareDate ==0){return p2.OrderID.CompareTo(p1.OrderID);}return compareDate;});


    his would give you ascending dates with descending orderIds. However, I wouldn't recommend sticking delegates as it will mean lots of places without code re-use. You should implement an IComparer and just pass that through to your Sort method. See here. public class MyOrderingClass : IComparer<Order> { public int Compare(Order x, Order y) { int compareDate = x.Date.CompareTo(y.Date); if (compareDate == 0) { return x.OrderID.CompareTo(y.OrderID); } return compareDate; } } And then to use this IComparer class, just instantiate it and pass it to your Sort method: IComparer<Order> comparer = new MyOrderingClass(); orderList.Sort(comparer);
  • 相关阅读:
    [数据结构] N皇后问题
    [2011山东ACM省赛] Sequence (动态规划)
    yaf 学习
    nginx 配置文件
    nginx.conf 理解
    fastcgi
    Nginx+FastCGI运行原理
    ssh-key 原理
    Git是个啥 ssh是个啥
    Git SSH Key 生成步骤
  • 原文地址:https://www.cnblogs.com/xpvincent/p/2847819.html
Copyright © 2011-2022 走看看