明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 884|回复: 0

[界面] ListView 中带有数字的字符串排序问题?

[复制链接]
发表于 2015-9-16 01:50 | 显示全部楼层 |阅读模式
本帖最后由 carrot1983 于 2015-9-16 01:52 编辑

http://bbs.csdn.net/topics/310046331

上面有,但是不知道为什么用不起来。请指点一二。

https://msdn.microsoft.com/zh-cn ... viewitemsorter.aspx
因为是按字符串排序,遇到数字时就变成,1,100,110,112的排序了。
        #region ListView排序类
        /// <summary>
        /// ListView排序类
        /// </summary>
        public class ListViewItemComparer : System.Collections.IComparer
        {
            //private int col;
            private SortOrder order;

            public ListViewItemComparer()
            {
                //col = 0;
                order = SortOrder.Ascending;
            }
            public ListViewItemComparer(int column, SortOrder order)
            {
                //col = column;
                this.order = order;
            }
            public int Compare(object x, object y)
            {
                int returnVal = -1;
                int col = 0;

                if (ValidateNum(((ListViewItem)x).SubItems[col].Text) && ValidateNum(((ListViewItem)y).SubItems[col].Text))
                {
                    returnVal = decimal.Compare(Convert.ToDecimal(((ListViewItem)x).SubItems[col].Text),
                        Convert.ToDecimal(((ListViewItem)y).SubItems[col].Text));

                    if (order == SortOrder.Descending)
                        returnVal *= -1;
                    return returnVal;
                }
                else
                {
                    returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);

                    if (order == SortOrder.Descending)
                        returnVal *= -1;
                    return returnVal;
                }
            }

        }

        public static bool ValidateNum(string InputStr)
        {
            if (InputStr != "")
            {
                if (Regex.IsMatch(InputStr.Trim(' '), @"^(0|[+-]?0\.\d+|[+-]?[1-9]([0-9]+)?(\.\d+)?)$"))
                    return true;
                else
                    return false;
            }
            return true;
        }
        #endregion

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-2 05:50 , Processed in 0.279698 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表