欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

DirectInfo.GetFiles返回数组的默认排序示例

程序员文章站 2024-02-25 11:45:34
复制代码 代码如下:using system;using system.collections;using system.io; namespace consoleapp...

复制代码 代码如下:

using system;
using system.collections;
using system.io;

namespace consoleapplication1
{
    /// <summary>
    /// 文件排序类
    /// </summary>
    public class filesort : icomparer
    {
        private fileorder _fileorder;
        private fileasc _fileasc;

        /// <summary>
        /// 构造函数
        /// </summary>
        public filesort()
            : this(fileorder.name, fileasc.asc)
        { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="fileorder"></param>
        public filesort(fileorder fileorder)
            : this(fileorder, fileasc.asc)
        { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="fileorder"></param>
        /// <param name="fileasc"></param>
        public filesort(fileorder fileorder, fileasc fileasc)
        {
            _fileorder = fileorder;
            _fileasc = fileasc;
        }

        /// <summary>
        /// 比较函数
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int compare(object x, object y)
        {
            fileinfo file1 = x as fileinfo;
            fileinfo file2 = y as fileinfo;
            fileinfo file3;

            if (file1 == null || file2 == null)
                throw new argumentexception("参数不是fileinfo类实例.");

            if (_fileasc == fileasc.desc)
            {
                file3 = file1;
                file1 = file2;
                file2 = file3;
            }

            switch (_fileorder)
            {
                case fileorder.name:
                    return file1.name.compareto(file2.name);
                case fileorder.length:
                    return file1.length.compareto(file2.length);
                case fileorder.extension:
                    return file1.extension.compareto(file2.extension);
                case fileorder.creationtime:
                    return file1.creationtime.compareto(file2.creationtime);
                case fileorder.lastaccesstime:
                    return file1.lastaccesstime.compareto(file2.lastaccesstime);
                case fileorder.lastwritetime:
                    return file1.lastwritetime.compareto(file2.lastwritetime);
                default:
                    return 0;
            }
        }
    }

    /// <summary>
    /// 排序依据
    /// </summary>
    public enum fileorder
    {
        /// <summary>
        /// 文件名
        /// </summary>
        name,
        /// <summary>
        /// 大小
        /// </summary>
        length,
        /// <summary>
        /// 类型
        /// </summary>
        extension,
        /// <summary>
        /// 创建时间
        /// </summary>
        creationtime,
        /// <summary>
        /// 访问时间
        /// </summary>
        lastaccesstime,
        /// <summary>
        /// 修改时间
        /// </summary>
        lastwritetime
    }

    /// <summary>
    /// 升序降序
    /// </summary>
    public enum fileasc
    {
        /// <summary>
        /// 升序
        /// </summary>
        asc,
        /// <summary>
        /// 降序
        /// </summary>
        desc
    }
}