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