C# 中 List.Sort運用(IComparer )排序用法


        /// <summary>
        /// 比較人物類實例大小,實現接口IComparer
        /// </summary>
        public class InternetProtocolComparer : IComparer<InternetProtocol>
        {
            public int Compare(InternetProtocol x, InternetProtocol y)
            {
                if (x == null)
                {
                    if (y == null)
                        return 0;
                    else
                        return -1;
                }
                else
                {
                    if (y == null)
                        return 1;
                    else
                    {

  

              if (string.IsNullOrWhiteSpace(x.IP) || string.IsNullOrWhiteSpace(y.IP))
                return 0;

                        int xIP = int.Parse(x.IP.Replace(".", ""));
                        int yIP = int.Parse(y.IP.Replace(".", ""));

                        int retval = yIP.CompareTo(xIP);

                        return retval;
                    
                    }
                }
            }
        }

  參考地址:http://blog.csdn.net/kongwei521/article/details/12133377


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM