C# Windows目录排序

转载:https://blog.csdn.net/zhou_ml/article/details/1602629
Windows目录排序以及类排序
1.目录排序

 	//Windows中排序优先级 特殊符号>数字>字母>字符
    //其中对数字的处理比较特别;如:10a,1a进行比较,1a 的优先级要大于10a,这是因为windows排序把1a拆分为 1和a, 10a拆分为 10和a然后分别比较。如果用字典排序,10a的优先级要比1a的优先级高,
    public static class  WinSort
    {
        public static int StringCompare(this 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;
        }

        public static 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;
        }

        public static int DigitalStringCompare(string A, string B)
        {
            int a = Convert.ToInt32(A);
            int 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;
        }

        public static 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;
        }
    }

再加个比较:
2.对象比较

public class Dog
{
	public string Name{get;set;}
}

public DogCompare:Icomparer<Dog>
{
	public int Compare(Dog a,Dog b)
	{
		return a.Name.Compare(y.Name);
	}
}

List<Dog> dogs = new List<Dog>();
dogs.Add(new Dog());
//3种写法
//1
DogCompare dc = new DogCompare();
dogs.Sort(0,dogs.Count,dc); 
//dogs.Sort(dc);
//2
//dogs.Sort((x,y)=>x.Name.Compare(y.Name,true));
//3
//dogs.Sort(delegate(Dog a,Dog b){return a.Name.Compare(b.Name);});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值