zoukankan      html  css  js  c++  java
  • IComparer实现文件名排序

    一、概要

     

    本文主要分享在文件对象处理中需要根据文件名排序思路。主要基于.net框架内提供的IComparer对象,它主要将定义类型为比较两个对象而实现的方法。

    二、详细内容

    1.场景

    在读取文件列表的时候,会遇到各种各样的文件名例如“xxx-01”,"1xx01-13"希望按照数字的大小进行排序;可是一旦文件名中出现了符号那么排序就不能用List集合提供Sort方法的常规排序规则了。这时候就会出现文件名顺序乱掉的情况。这个时候我们就需要重新定义这个排序规则了。那么刚好IComparer就可以支持我们来解决这个问题。

    2.实现

    IComparer的实现

    public class FilesNameComparer<T> : IComparer<T>
    {
    public int Compare(T x, T y)
    {
    if (x == null || y == null)
    throw new ArgumentException("Parameters can't be null");

    //对比文件对象A的文件名
    string fileA = x.ToString();
    //对比文件对象B的文件名
    string fileB = y.ToString();

    //将文件名里的字符一个个拆成字符数组
    char[] arr1 = fileA.ToCharArray();
    char[] arr2 = fileB.ToCharArray();

    int i = 0, j = 0;
    //逐字符处理
    while (i < arr1.Length && j < arr2.Length)
    {
    if (char.IsDigit(arr1[i]) && char.IsDigit(arr2[j]))
    {
    string s1 = "", s2 = "";
    while (i < arr1.Length && char.IsDigit(arr1[i]))
    {
    s1 += arr1[i];
    i++;
    }
    while (j < arr2.Length && char.IsDigit(arr2[j]))
    {
    s2 += arr2[j];
    j++;
    }
    if (int.Parse(s1) > int.Parse(s2)) return 1;
    if (int.Parse(s1) < int.Parse(s2)) return -1;
    }
    else
    {
    if (arr1[i] > arr2[j]) return 1;
    if (arr1[i] < arr2[j]) return -1;
    i++;
    j++;
    }
    }

    if (arr1.Length == arr2.Length)
    {
    return 0;
    }
    else
    {
    return arr1.Length > arr2.Length ? 1 : -1;
    }
    }
    }

    FileModel的实现

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

    public override string ToString()
    {
    return Name;
    }
    }

    调用

    var comparer = new FilesNameComparer<FileModel>();
    List<FileModel> fileList = new List<FileModel>();
    fileList.Add(new FileModel { Name = "100-1" });
    fileList.Add(new FileModel { Name = "1" });
    fileList.Add(new FileModel { Name = "101-1" });
    fileList.Add(new FileModel { Name = "101-2" });
    fileList.Add(new FileModel { Name = "100-2" });
    fileList.Add(new FileModel { Name = "102-3" });
    fileList.Add(new FileModel { Name = "110-1" });
    fileList.Add(new FileModel { Name = "20-1" });
    fileList.Sort(comparer);
    foreach (var s in fileList)
    {
    Console.WriteLine(s.Name);
    }

    3.运行效果

  • 相关阅读:
    C# extern关键字的用法
    C#自定义集合类(二)
    C#自定义集合类(一)
    LINQ中交集、并集、差集、去重(十四)
    LINQ中转换操作符(十三)
    Oracle实现连乘和求和
    适配器模式
    HTTP网络协议与手写Web服务容器
    代理模式
    设计模式的几条家规
  • 原文地址:https://www.cnblogs.com/justzhuzhu/p/14939326.html
Copyright © 2011-2022 走看看