例如:
目录D:\Test下有很多文件
类似 Hello1.jpg Hello2.png, Hello3.bmp ... Hello20.psd .. Test1.c ... Test30.cpp
通常我们读取目录下文件列表后,得到文件名排序是按照下列方式:
Hello3.xxx
Hello4.xxx
Hello5.xxx
Hello6.xxx
Hello7.xxx
Hello8.xxx
Hello9.xxx
Hello1.xxx
Hello10.xxx
Hello2.xxx
Hello20.xxx
Hello21.xxx即不是按照我们预想的那种按照末尾数字大小来排序的方式:
Hello1.xxx
Hello2.xxx
Hello3.xxx
Hello4.xxx
Hello5.xxx
Hello6.xxx
Hello7.xxx
Hello8.xxx
Hello9.xxx
Hello10.xxx
Hello20.xxx
Hello21.xxx下面要求写出一个方法,实现输入第一种排序方式的字符串数组输出第二种方式的结果,扩展名xxx表示任意。public static string[] SortFileName(string[] input)
{
    // TODO:你的实现
}

解决方案 »

  1.   

    取得文件主名,取末尾的数字,转换为 int,排序。
      

  2.   

    哈哈,我没说清楚,文件名是任意的,不一定前缀都是Hello
    有可能是
    a1.jpg
    a2.jpg
    a10.jpg
    b3.bmp
    b4.png
    b10.jpg
      

  3.   

    using System;
    using System.Text.RegularExpressions;class Program
    {
      public static string[] SortFileName(string[] input)
      {
        Regex r = new Regex(@"[0-9]+(?=\.[^.]+$)");
        decimal[] key = new decimal[input.Length];
        for (int i = 0; i < key.Length; i++)
        {
          key[i] = decimal.Parse(r.Match(input[i]).Value);
        }
        Array.Sort(key, input);
        return input;
      }  static void Main()
      {
        string[] a = { "Hello3.xxx", "Hello4.xxx", "Hello9.xxx", "Hello1.xxx", 
          "Hello10.xxx", "Hello2.xxx", "Hello20.xxx", "Hello21.xxx", "Hello100.xxx", };
        string[] b = SortFileName(a);
        foreach (string s in b)
        {
          Console.WriteLine(s);
        }
      }
    }
    /* 程序输出:
    Hello1.xxx
    Hello2.xxx
    Hello3.xxx
    Hello4.xxx
    Hello9.xxx
    Hello10.xxx
    Hello20.xxx
    Hello21.xxx
    Hello100.xxx
    */
      

  4.   

    using System;
    using System.Text.RegularExpressions;class Program
    {
      public static string[] SortFileName(string[] input)
      {
        Regex r = new Regex(@"^([^.0-9]*)([0-9]*)\.[^.]+$");
        string[] key = new string[input.Length];
        for (int i = 0; i < key.Length; i++)
        {
          Match m = r.Match(input[i]);
          key[i] = string.Format("{0}{1:d8}", m.Groups[1].Value, int.Parse(m.Groups[2].Value));
        }
        Array.Sort(key, input);
        return input;
      }  static void Main()
      {
        string[] a = { "a1.jpg", "a10.jpg", "b10.jpg", "b4.png", "a2.jpg", "b3.bmp" };
        string[] b = SortFileName(a);
        foreach (string s in b)
        {
          Console.WriteLine(s);
        }
      }
    }
    /* 程序输出:
    a1.jpg
    a2.jpg
    a10.jpg
    b3.bmp
    b4.png
    b10.jpg
    */
      

  5.   

    完善一下,这样即使文件名不含数字也不会出错:
    using System;
    using System.Text.RegularExpressions;class Program
    {
      public static string[] SortFileName(string[] input)
      {
        Regex r = new Regex(@"^([^.0-9]*)([0-9]*)\.[^.]+$");
        string[] key = new string[input.Length];
        for (int i = 0; i < key.Length; i++)
        {
          Match m = r.Match(input[i]);
          key[i] = m.Groups[1].Value;
          if (m.Groups[2].Length > 0)
            key[i] += int.Parse(m.Groups[2].Value).ToString("d10");
        }
        Array.Sort(key, input);
        return input;
      }  static void Main()
      {
        string[] a = { "B.jpg", "a10.jpg", "B10.jpg", "100.png", "A2.jpg", "b3.bmp" };
        string[] b = SortFileName(a);
        foreach (string s in b)
        {
          Console.WriteLine(s);
        }
      }
    }
    /* 程序输出:
    100.png
    A2.jpg
    a10.jpg
    B.jpg
    b3.bmp
    B10.jpg
    */
      

  6.   

      public static string[] SortFileName(string[] input)
      {
        Regex r = new Regex(@"(\D*)(\d*)");
        string[] key = new string[input.Length];
        for (int i = 0; i < key.Length; i++)
        {
          Match m = r.Match(Path.GetFileNameWithoutExtension(input[i]));
          key[i] = m.Groups[1].Value + m.Groups[2].Value.PadLeft(64);
        }
        Array.Sort(key, input);
        return input;
      }
      

  7.   

    凑个热闹        public static string[] SortFileName(string[] input)
            {
                Array.Sort(input, new Comparison<string>((s1, s2) =>
                {
                    string ext1 = Path.GetExtension(s1);
                    string ext2 = Path.GetExtension(s2);
                    if (ext1 != ext2)
                        return ext1.CompareTo(ext2);
                    string file1 = Path.GetFileNameWithoutExtension(s1);
                    string file2 = Path.GetFileNameWithoutExtension(s2);
                    Match m1 = Regex.Match(file1, "\\d+");
                    Match m2 = Regex.Match(file2, "\\d+");
                    while (m1 != null && m2 != null && !string.IsNullOrEmpty(m1.Value) && !string.IsNullOrEmpty(m2.Value))
                    {
                        if (m1.Value != m2.Value)
                            return int.Parse(m1.Value).CompareTo(int.Parse(m2.Value));
                        m1 = m1.NextMatch();
                        m2 = m2.NextMatch();
                    }
                    return file1.CompareTo(file2);
                }));
                return input;
            }