找回密码
 立即注册

QQ登录

只需一步,快速开始

qimeimeiqi

初级会员

12

主题

66

帖子

242

积分

初级会员

积分
242

微信认证勋章

[已处理] spread的数据排序

qimeimeiqi
初级会员   /  发表于:2019-10-16 16:46  /   查看:6699  /  回复:11
5金币
本帖最后由 qimeimeiqi 于 2019-10-16 16:52 编辑

现在的状态是,第二主键的排序顺序是自然顺序(例 1,2,11)   第一主键的排序顺序是(例 1,11,2)
想要把第一主键的排序变更为自然顺序,应该如何修改。
谢谢

代码如下
private int CompareCodeMaster(Model.CodeMaster x, Model.CodeMaster y)
        {
            // 第一主键的比較
            int compareCodeType = string.Compare(x.CodeType, y.CodeType);     

            if (compareCodeType == 0)
            {
                第一主键相同时,第二主键的比较
                return NaturalComparer(x.Code, y.Code);
            }
            else
            {
                return compareCodeType;
            }

        }

        private static int NaturalComparer(string x, string y)
        {
            Dictionary<string, string[]> table = new Dictionary<string, string[]>();

            if ((x != null) && (y != null))
            {
                if (x == y)
                {
                    return 0;
                }
                string[] x1, y1;
                if (!table.TryGetValue(x, out x1))
                {
                    x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
                    table.Add(x, x1);
                }
                if (!table.TryGetValue(y, out y1))
                {
                    y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
                    table.Add(y, y1);
                }

                for (int i = 0; i < x1.Length && i < y1.Length; i++)
                {
                    if (x1 != y1)
                    {
                        return PartCompare(x1, y1);
                    }
                }
                if (y1.Length > x1.Length)
                {
                    return 1;
                }
                else if (x1.Length > y1.Length)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
        private static int PartCompare(string left, string right)
        {
            int x, y;
            if (!int.TryParse(left, out x))
            {
                return left.CompareTo(right);
            }

            if (!int.TryParse(right, out y))
            {
                return left.CompareTo(right);
            }

            return x.CompareTo(y);
        }

11 个回复

倒序浏览
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-10-16 17:48:59
沙发

  1.             FarPoint.Win.Spread.SortInfo[] si = new SortInfo[1];
  2.             si[0] = new SortInfo(0, true, System.Collections.Comparer.Default);

  3.             fpSpread1.ActiveSheet.SortRows(0,fpSpread1.ActiveSheet.RowCount,si );
复制代码


自定义一个IComparer,写到sortInfo中
回复 使用道具 举报
qimeimeiqi
初级会员   /  发表于:2019-10-17 09:35:59
板凳
dexteryao 发表于 2019-10-16 17:48
自定义一个IComparer,写到sortInfo中

能稍微具体点吗?上面那个我没做出来,运行后的效果没有变化
谢谢
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-10-17 09:57:59
地板
回复 使用道具 举报
qimeimeiqi
初级会员   /  发表于:2019-10-17 14:56:42
5#
本帖最后由 qimeimeiqi 于 2019-10-18 13:22 编辑

我还是没弄出来
IComparer.Comparer和SortInfo如何相连接,
自定义IComparer后,如何与我本身的数据相连接,
把网址的内容稍稍改动之后,会出现错误现象,
如果不改动,又没有效果
求指导,谢谢
注‘第一主键为codetype,第二主键为code’
现在改动出来的
FarPoint.Win.Spread.SortInfo[] si = new SortInfo[1];
            si[0] = new SortInfo(0, true, System.Collections.Comparer.Default);
            int compareCodeType;      
            compareCodeType = string.Compare(x.CodeType, y.CodeType);
            spreadCodeMaster.ActiveSheet.SortRows(0, spreadCodeMaster.ActiveSheet.RowCount, si);
private static int IComparer(string x, string y)
        {
            Dictionary<string, string[]> table = new Dictionary<string, string[]>();

            if ((x != null) && (y != null))
            {
                if (x == y)
                {
                    return 0;
                }
                string[] x1, y1;
                if (!table.TryGetValue(x, out x1))
                {
                    x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
                    table.Add(x, x1);
                }
                if (!table.TryGetValue(y, out y1))
                {
                    y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
                    table.Add(y, y1);
                }

                for (int i = 0; i < x1.Length && i < y1.Length; i++)
                {
                    if (x1 != y1)
                    {
                        return PartCompare(x1, y1);
                    }
                }
                if (y1.Length > x1.Length)
                {
                    return 1;
                }
                else if (x1.Length > y1.Length)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }


回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-10-21 12:09:45
6#
您发个Demo吧,在您的Demo上我看看有什么问题
回复 使用道具 举报
qimeimeiqi
初级会员   /  发表于:2019-10-21 15:20:46
7#
dexteryao 发表于 2019-10-21 12:09
您发个Demo吧,在您的Demo上我看看有什么问题

我想知道demo怎么发,为什么添加附件这一栏中不支持我想上传的文件谢谢
回复 使用道具 举报
qimeimeiqi
初级会员   /  发表于:2019-10-21 15:54:36
8#
本帖最后由 qimeimeiqi 于 2019-10-21 15:57 编辑
dexteryao 发表于 2019-10-21 12:09
您发个Demo吧,在您的Demo上我看看有什么问
スクリーンショット (12).png
灰色部分是主键,左边是第一主键,右边是第二主键
第二主键现在的排序是自然排序(eg:4,14,24)
第一主键的排序(eg:1,2,21,3,4)

  private int CompareCodeMaster(Model.CodeMaster x, Model.CodeMaster y)
        {
            // 第一のキー(コード種別)で比較
            int compareCodeType = string.Compare(x.CodeType, y.CodeType);

            if (compareCodeType == 0)
            {
                // コード種別が同じだった場合、第二のキー(コード値)で比較する
                return NaturalComparer(x.Code, y.Code);
            }
            else
            {
                // x.CodeType > y.CodeTypeもしくはx.CodeType < y.CodeTypeだった場合、比較結果をそのまま返す
                return compareCodeType;
            }
        }
        private static int NaturalComparer(string x, string y)
        {
            Dictionary<string, string[]> table = new Dictionary<string, string[]>();

            if ((x != null) && (y != null))
            {
                if (x == y)
                {
                    return 0;
                }
                string[] x1, y1;
                if (!table.TryGetValue(x, out x1))
                {
                    x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
                    table.Add(x, x1);
                }
                if (!table.TryGetValue(y, out y1))
                {
                    y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
                    table.Add(y, y1);
                }

                for (int i = 0; i < x1.Length && i < y1.Length; i++)
                {
                    if (x1 != y1)
                    {
                        return PartCompare(x1, y1);
                    }
                }
                if (y1.Length > x1.Length)
                {
                    return 1;
                }
                else if (x1.Length > y1.Length)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
        private static int PartCompare(string left, string right)
        {
            int x, y;
            if (!int.TryParse(left, out x))
            {
                return left.CompareTo(right);
            }

            if (!int.TryParse(right, out y))
            {
                return left.CompareTo(right);
            }

            return x.CompareTo(y);
        }

スクリーンショット (11).png
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-10-22 11:37:36
9#
您好,您这个还是代码片段,这个代码是在什么时候调用的?怎么调用的?

您可以看下另一个客户的方案:
https://gcdn.grapecity.com.cn/fo ... 6&fromuid=31720
回复 使用道具 举报
qimeimeiqi
初级会员   /  发表于:2019-10-23 17:36:24
10#
本帖最后由 qimeimeiqi 于 2019-10-24 16:47 编辑


回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部