-2

我的文件夹中有文件,其名称如下:

"C:\\Users\\John\\Documents\\333\\12.html"

如何对它们进行排序以使 2.html 出现在 10.html 之前?

4

3 回答 3

3

对文件进行排序时,将字符串解析为数字。

例子:

string[] files = {
  "2.html",
  "10.html",
  "1.html"
};

files =
  files.OrderBy(s => Int32.Parse(s.Substring(0, s.IndexOf('.'))))
  .ToArray();
于 2012-04-19T12:34:56.540 回答
2
    Directory
      .GetFiles()
      .OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f)) )
于 2012-04-19T12:35:22.250 回答
0

看看这个帖子——

http://www.codeproject.com/Articles/11016/Numeric-String-Sort-in-C

string[] files = System.IO.Directory.GetFiles();

NumericComparer ns = new NumericComparer();
Array.Sort(files, ns);

// files will now sorted in the numeric order
// we can do the same for directories

string[] dirs = System.IO.Directory.GetDirectories();
ns = new NumericComparer(); // new object
Array.Sort(dirs, ns);

public class NumericComparer : IComparer
    {
        public NumericComparer()
        {}

        public int Compare(object x, object y)
        {
            if((x is string) && (y is string))
            {
                return StringLogicalComparer.Compare((string)x, (string)y);
            }
            return -1;
        }
    }//EOC
于 2012-04-19T12:32:49.800 回答