paip.目录文件列表排序算法

paip.目录文件列表排序算法


----作者Attilax , 1466519819@qq.com---  
今天要给一个目录下文件排序。但是效果总是不WIN7的不同。它是按字符来排序的……




实际的排序是这样的。


先排列数字文件列表。按数字大小排。




然后非数字文件名列表……按字符比较来排。




FileSystemInfo [] files = dirinfo.GetFileSystemInfos();


System.Collections.Generic.List<FileInfo> li=toList(files);
 
 Comparison<FileInfo> com = new Comparison<FileInfo>(Compare);
 li.Sort(com);






























private int Compare(FileInfo info1, FileInfo info2)
{




    String filenameNoEx = Path.GetFileNameWithoutExtension(info1.Name);
    String filenameNoEx2 = Path.GetFileNameWithoutExtension(info2.Name);
    if( m.str.isNum(filenameNoEx))
        if (m.str.isNum(filenameNoEx2))
        {
            int one = Int32.Parse(filenameNoEx.ToString() );
            int two = Int32.Parse(filenameNoEx2.ToString() );
            if (two > one)
                return -1;
            else
                return 1;
        }
    //try
    //{
    //    int one = Int32.Parse(info1.Name.ToString().Replace(".jpg", ""));
    //    int two = Int32.Parse(info2.Name.ToString().Replace(".jpg", ""));
    //    if (two > one)
    //        return -1;
    //    else
    //        return 1;
    //}
    //catch (Exception ex) { }
    return 0;
    int result;
    CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer();
    result = ObjectCompare.Compare(info1.FullName, info2.FullName);
    return result;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值