Пример #1
0
        public int Compare(object x, object y)
        {
            int      returnVal = -1;
            DateTime xtime, ytime;
            long     xlong, ylong;
            string   xin, yin;

            xin = ((ListViewItem)x).SubItems[SortColumn].Text;
            yin = ((ListViewItem)y).SubItems[SortColumn].Text;
            if (DateTime.TryParse(xin, out xtime) && DateTime.TryParse(yin, out ytime))
            {
                returnVal = DateTime.Compare(xtime, ytime);
            }
            else if (long.TryParse(xin, out xlong) && long.TryParse(yin, out ylong))
            {
                returnVal = (int)(xlong - ylong);
            }
            else
            {
                returnVal = ObjectCompare.Compare(((ListViewItem)x).SubItems[SortColumn].Text,
                                                  ((ListViewItem)y).SubItems[SortColumn].Text);
            }
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {
                return(returnVal);
            }
            else
            {
                return(-1 * returnVal);
            }
        }
Пример #2
0
        public int Compare(ListViewItem listviewX, ListViewItem listviewY)
        {
            // Compare the two items, checking tags first
            int compareResult = 0;

            if (listviewX.SubItems[SortColumn].Tag != null && listviewY.SubItems[SortColumn].Tag != null)
            {
                compareResult = ObjectCompare.Compare(listviewX.SubItems[SortColumn].Tag, listviewY.SubItems[SortColumn].Tag);
            }
            else
            {
                compareResult = ObjectCompare.Compare(listviewX.SubItems[SortColumn].Text, listviewY.SubItems[SortColumn].Text);
            }

            return(AdjustCompareForOrder(compareResult));
        }
            public int Compare(ListViewItem listviewX, ListViewItem listviewY)
            {
                // Compare the two items
                int compareResult = ObjectCompare.Compare(listviewX.SubItems[SortColumn].Text, listviewY.SubItems[SortColumn].Text);

                // Calculate correct return value based on object comparison
                if (Order == SortOrder.Ascending)
                {
                    // Ascending sort is selected, return normal result of compare operation
                    return(compareResult);
                }
                else if (Order == SortOrder.Descending)
                {
                    // Descending sort is selected, return negative result of compare operation
                    return(-compareResult);
                }
                // Return '0' to indicate they are equal
                return(0);
            }
        /// <summary>
        /// 重写IComparer接口.
        /// </summary>
        /// <param name="x">要比较的第一个对象</param>
        /// <param name="y">要比较的第二个对象</param>
        /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
        public int Compare(object x, object y)
        {
            if (OrderOfSort == System.Windows.Forms.SortOrder.None)
            {
                return(0);
            }
            int compareResult = 0;

            System.Windows.Forms.ListViewItem listviewX, listviewY;
            // 将比较对象转换为ListViewItem对象
            listviewX = (System.Windows.Forms.ListViewItem)x;
            listviewY = (System.Windows.Forms.ListViewItem)y;
            string xText = listviewX.SubItems[ColumnToSort].Text;
            string yText = listviewY.SubItems[ColumnToSort].Text;
            int    xInt, yInt;

            // 比较,如果值为IP地址,则根据IP地址的规则排序。
            if (IsIP(xText) && IsIP(yText))
            {
                compareResult = CompareIp(xText, yText);
            }
            else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt)) //是否全为数字
            {
                //比较数字
                compareResult = CompareInt(xInt, yInt);
            }
            else if (IsFileSize(xText) && IsFileSize(yText))
            {
                compareResult = CompareFileSize(xText, yText);
            }
            else
            {
                //比较对象
                compareResult = ObjectCompare.Compare(xText, yText);
            }
            // 根据上面的比较结果返回正确的比较结果
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {
                // 因为是正序排序,所以直接返回结果
                return(compareResult);
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {
                // 如果是反序排序,所以要取负值再返回
                return(-compareResult);
            }
            else
            {
                // 如果相等返回0
                return(0);
            }
        }
Пример #5
0
            public int Compare(object x, object y)
            {
                var lvX    = x as ListViewItem;
                var lvY    = y as ListViewItem;
                int result = _objectCompare.Compare(lvX.SubItems[SortColumn].Text,
                                                    lvY.SubItems[SortColumn].Text);

                switch (Order)
                {
                case SortOrder.Ascending:
                    return(result);

                case SortOrder.Descending:
                    return(-result);

                default:
                    return(0);
                }
            }
Пример #6
0
        public int Compare(object x, object y)
        {
            ListViewItem listviewX     = (ListViewItem)x;
            ListViewItem listviewY     = (ListViewItem)y;
            int          compareResult = ObjectCompare.Compare(
                listviewX.SubItems.Count > SortColumn ? listviewX.SubItems[SortColumn].Text : null,
                listviewY.SubItems.Count > SortColumn ? listviewY.SubItems[SortColumn].Text : null);

            if (Order == SortOrder.Ascending)
            {
                return(compareResult);
            }
            else if (Order == SortOrder.Descending)
            {
                return(-compareResult);
            }
            else
            {
                return(0);
            }
        }
Пример #7
0
    /// <summary>
    /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
    /// </summary>
    /// <param name="x">First object to be compared</param>
    /// <param name="y">Second object to be compared</param>
    /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
    public int Compare(object x, object y)
    {
        try
        {
            int compareResult;
            System.Windows.Forms.ListViewItem listviewX, listviewY;

            // Cast the objects to be compared to ListViewItem objects
            listviewX = (System.Windows.Forms.ListViewItem)x;
            listviewY = (System.Windows.Forms.ListViewItem)y;

            // Compare the two items
            compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);

            // Calculate correct return value based on object comparison
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {
                // Ascending sort is selected, return normal result of compare operation
                return(compareResult);
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {
                // Descending sort is selected, return negative result of compare operation
                return(-compareResult);
            }
            else
            {
                // Return '0' to indicate they are equal
                return(0);
            }
        }
        catch
        {
            return(0);
        }
    }
Пример #8
0
        /// <summary>
        /// 重写IComparer接口.
        /// </summary>
        /// <param name="x">要比较的第一个对象</param>
        /// <param name="y">要比较的第二个对象</param>
        /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
        public int Compare(object x, object y)
        {
            int compareResult;

            System.Windows.Forms.ListViewItem listviewX, listviewY;

            // 将比较对象转换为ListViewItem对象
            listviewX = (System.Windows.Forms.ListViewItem)x;
            listviewY = (System.Windows.Forms.ListViewItem)y;

            if (listviewX.SubItems[0].Text.Equals("上级目录") || listviewY.SubItems[0].Text.Equals("上级目录"))
            {
                return(0);
            }

            string xText = listviewX.SubItems[ColumnToSort].Text;
            string yText = listviewY.SubItems[ColumnToSort].Text;

            int      xInt, yInt;
            DateTime xDt, yDt;

            // 比较,如果值为IP地址,则根据IP地址的规则排序。
            if (IsIP(xText) && IsIP(yText))
            {
                compareResult = CompareIp(xText, yText);
            }
            else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt)) //是否全为数字
            {
                //比较数字
                compareResult = CompareInt(xInt, yInt);
            }
            else if (DateTime.TryParse(xText, out xDt) && DateTime.TryParse(yText, out yDt))//判断是否为日期数据
            {
                //比较时间
                compareResult = DateTime.Compare(yDt, xDt);
            }
            //else if (xText.Substring(xText.Length - 1, 1).Equals("B") && yText.Substring(yText.Length - 1, 1).Equals("B")) //判断是否为容量单位
            //{
            //    //比较容量大小
            //    xInt = Convert.ToInt32(getByteSize(xText));
            //    yInt = Convert.ToInt32(getByteSize(yText));
            //    compareResult = CompareInt(xInt, yInt);
            //}
            //else if (xText.Substring(xText.Length - 1, 1).Equals(" ") && yText.Substring(yText.Length - 1, 1).Equals("B"))
            //{
            //    xInt = Convert.ToInt32(getByteSize(xText));
            //    yInt = Convert.ToInt32(getByteSize(yText));
            //    compareResult = CompareInt(xInt, yInt);
            //}
            //else if (xText.Substring(xText.Length - 1, 1).Equals("B") && yText.Substring(yText.Length - 1, 1).Equals(" "))
            //{
            //    xInt = Convert.ToInt32(getByteSize(xText));
            //    yInt = Convert.ToInt32(getByteSize(yText));
            //    compareResult = CompareInt(xInt, yInt);
            //}
            //else if (xText.Substring(xText.Length - 1, 1).Equals(" ") && yText.Substring(yText.Length - 1, 1).Equals(" "))
            //{
            //    xInt = Convert.ToInt32(getByteSize(xText));
            //    yInt = Convert.ToInt32(getByteSize(yText));
            //    compareResult = CompareInt(xInt, yInt);
            //}
            else
            {
                //比较对象
                compareResult = ObjectCompare.Compare(xText, yText);
            }

            // 根据上面的比较结果返回正确的比较结果
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {
                // 因为是正序排序,所以直接返回结果
                return(compareResult);
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {
                // 如果是反序排序,所以要取负值再返回
                return(-compareResult);
            }
            else
            {
                // 如果相等返回0
                return(0);
            }
        }
Пример #9
0
        /// <summary>
        /// 重写IComparer接口
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
        public int Compare(object x, object y)
        {
            int compareResult;

            System.Windows.Forms.ListViewItem listviewX, listviewY;
            // 将比较对象转换为ListViewItem对象
            listviewX = (System.Windows.Forms.ListViewItem)x;
            listviewY = (System.Windows.Forms.ListViewItem)y;
            // 比较
            switch (this.DateType)
            {
            case "datetime":
            {
                compareResult = System.DateTime.Compare(
                    System.DateTime.Parse(listviewX.SubItems[ColumnToSort].Text),
                    System.DateTime.Parse(listviewY.SubItems[ColumnToSort].Text)
                    );
            }
            break;

            case "filesize":
            {
                System.Collections.CaseInsensitiveComparer ObjectCompare = new System.Collections.CaseInsensitiveComparer();        // 初始化CaseInsensitiveComparer类对象
                compareResult = ObjectCompare.Compare(
                    int.Parse(listviewX.SubItems[ColumnToSort].Text.Replace(",", "").Replace("KB", "").TrimEnd()),
                    int.Parse(listviewY.SubItems[ColumnToSort].Text.Replace(",", "").Replace("KB", "").TrimEnd())
                    );
            }
            break;

            case "imagesize":
            {
                System.Collections.CaseInsensitiveComparer ObjectCompare = new System.Collections.CaseInsensitiveComparer();        // 初始化CaseInsensitiveComparer类对象
                compareResult = ObjectCompare.Compare(
                    int.Parse(listviewX.SubItems[ColumnToSort].Text.Split('x')[0].TrimEnd()),
                    int.Parse(listviewY.SubItems[ColumnToSort].Text.Split('x')[0].TrimEnd())
                    );
            }
            break;

            case "string":
            default:
            {
                System.Collections.CaseInsensitiveComparer ObjectCompare = new System.Collections.CaseInsensitiveComparer();        // 初始化CaseInsensitiveComparer类对象
                compareResult = ObjectCompare.Compare(
                    listviewX.SubItems[ColumnToSort].Text,
                    listviewY.SubItems[ColumnToSort].Text
                    );
            }
            break;
            }
            // 根据上面的比较结果返回正确的比较结果
            if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
            {   // 因为是正序排序,所以直接返回结果
                return(compareResult);
            }
            else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
            {  // 如果是反序排序,所以要取负值再返回
                return(-compareResult);
            }
            else
            {
                // 如果相等返回0
                return(0);
            }
        }