模拟Windows排序的算法

下面一段C#代码是模拟Window排序的算法,不包括外围的排序,只是两个String的比较。

写的比较粗糙,请大家提出宝贵意见。

window中的排序,优先级是这样的,

特殊符号 〉 数字  〉 字母 〉 字符

其中对数字的处理比较特别举例如下:

10a,1a进行比较,1a 的优先级要大于10a,这是因为windows排序把1a拆分为 1和a, 10a拆分为 10和a然后分别比较。如果用字典排序,10a的优先级要比1a的优先级高,

-------------------------------------------------------------------------------------------------------------------

private int StringCompare(string A, string B, bool sortType)
        {
            string strA = string.Empty;
            string strB = string.Empty;
            int checkResult;
            int result;
            bool flagA;
            bool flagB;

            Dictionary<int, string> libA = new Dictionary<int, string>();
            Dictionary<int, string> libB = new Dictionary<int, string>();

            checkResult = CheckStringValue(A, B, sortType);
            if (2 != checkResult)
            {
                return checkResult;
            }

            CutString(A, 0, libA);
            CutString(B, 0, libB);

            // loop for compare string
            for (int i = 0; (i < libA.Count) && (i < libB.Count); ++i)
            {
                libA.TryGetValue(i, out strA);
                libB.TryGetValue(i, out strB);

                flagA = Char.IsLetterOrDigit(strA, 0);
                flagB = Char.IsLetterOrDigit(strB, 0);
                if (!flagA || !flagB)
                {
                    if (!flagA && !flagB)
                    {
                        result = string.Compare(strA, strB);
                    }
                    else
                    {
                        if (!flagA)
                        {
                            return -1;
                        }
                        else
                        {
                            return 1;
                        }
                    }

                }
                else
                {
                    flagA = Char.IsDigit(strA, 0);
                    flagB = Char.IsDigit(strB, 0);
                    //Both substrings is digit
                    if (flagA || flagB)
                    {
                        if (flagA && flagB)
                        {
                            result = DigitalStringCompare(strA, strB);
                        }
                        else
                        {
                            // Only strA is Digit
                            if (flagA)
                            {
                                result = -1;
                            }
                            else  // Only strB is Digit
                            {
                                result = 1;
                            }
                        }
                    }
                    else
                    {
                        result = string.Compare(strA, strB);
                    }

                    if (0 != result)
                    {
                        return result;
                    }
                }
            } //end for

            if (libA.Count == libB.Count)
            {
                return 0;
            }

            if (libA.Count > libB.Count)
            {
                return 1;
            }

            return -1;
        }

        private void CutString(string str, int beginIndex, Dictionary<int, string> lib)
        {
            string strTemp = String.Empty;
            int counter = beginIndex;

            if (!Char.IsLetterOrDigit(str, beginIndex))
            {
                while ((counter < (str.Length)) && !Char.IsLetterOrDigit(str, counter))
                {
                    strTemp += str.Substring(counter, 1);
                    ++counter;
                }
            }
            else
            {
                if (Char.IsDigit(str, beginIndex))
                {
                    while ((counter < (str.Length)) && Char.IsDigit(str, counter))
                    {
                        strTemp += str.Substring(counter, 1);
                        ++counter;
                    }

                }
                else
                {
                    while ((counter < (str.Length)) && !Char.IsDigit(str, counter))
                    {
                        strTemp += str.Substring(counter, 1);
                        ++counter;
                    }

                }
            }

            if (strTemp != string.Empty)
            {
                lib.Add(lib.Count, strTemp);
            }

            if (counter < (str.Length))
            {
                CutString(str, counter, lib);
            }

            return;
        }

        private int DigitalStringCompare(string A, string B)
        {
            int a;
            int b;

            a = Convert.ToInt32(A);
            b = Convert.ToInt32(B);

            if (a > b)
            {
                return 1;
            }

            if (a < b)
            {
                return -1;
            }

            if (A.Length < B.Length)
            {
                return 1;
            }

            if (A.Length > B.Length)
            {
                return -1;
            }

            return 0;
        }

        private short CheckStringValue(string A, string B, bool sortType)
        {
            if ((null == A) || (null == B))
            {
                if ((null == A) && (null == B))
                {
                    return 0;
                }

                if (null == A)
                {
                    if (sortType)
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }

                if (null == B)
                {
                    if (sortType)
                    {
                        return -1;
                    }
                    else
                    {
                        return 1;
                    }
                }
            }
            else
            {
                if ((string.Empty == A) && (string.Empty == B))
                {
                    return 0;
                }

                if (string.Empty == A)
                {
                    if (sortType)
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }

                if (string.Empty == B)
                {
                    if (sortType)
                    {
                        return -1;
                    }
                    else
                    {
                        return 1;
                    }
                }
            }

            return 2;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值