请教一个文件排序问题

zxiangsen 2009-01-12 05:00:15
有这么几个文件,如file1,file2,file3,file4,file10,file12,file22,file23,用c#中的Directory.GetFiles()得到的文件数组默认情况下的排序是
file1
file10
file12
file2
file22
file23
file3
file4
....
请教各位大虾,有没有什么好办法让它安这样排序:
file1
file2
file3
file4
file10
file12
file22
file23
......
谢谢了。
...全文
73 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxxx2967 2009-01-13
  • 打赏
  • 举报
回复
首先实现一个排序类,实现你的排序算法,如下:

public class fileComparter : Comparer<string>
{
public override int Compare(string x, string y)
{
if (x.Length < y.Length)
{
return -1;
}
else if (x.Length > y.Length)
{
return 1;
}
else
{
return x.CompareTo(y);
}
}
}


在需要排序的地方调用,例如:
   List<string> Files = new List<string>();
Files.AddRange(Directory.GetFiles(Directory.GetCurrentDirectory()));
Files.Sort(new fileComparter());
foreach (string file in Files)
{
Console.WriteLine(file);
}

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧