当前位置:首页 > 通信资讯 > 正文

文件怎么按创建时间排序(c语言文件排序)

快速排序类

  1. usingSystem;
  2. usingSystem.Data;
  3. usingSystem.Configuration;
  4. usingSystem.Web;
  5. usingSystem.Web.Security;
  6. usingSystem.Web.UI;
  7. usingSystem.Web.UI.WebControls;
  8. usingSystem.Web.UI.WebControls.WebParts;
  9. usingSystem.Web.UI.HtmlControls;
  10. usingSystem.IO;
  11. ///<summary>
  12. ///快速排序算法
  13. ///</summary>
  14. publicclassMyQuickSort
  15. {
  16. publicMyQuickSort()
  17. {
  18. //
  19. //TODO:Addconstructorlogichere
  20. //
  21. }
  22. ///<summary>
  23. ///快速排序算法
  24. ///</summary>
  25. ///快速排序为不稳定排序,时间复杂度O(nlog2n),为同数量级中最快的排序方法
  26. ///<paramname="arr">划分的数组</param>
  27. ///<paramname="low">数组低端上标</param>
  28. ///<paramname="high">数组高端下标</param>
  29. ///<returns></returns>
  30. staticintPartition(FileInfo[]arr,intlow,inthigh)
  31. {
  32. //进行一趟快速排序,返回中心轴记录位置
  33. //arr[0]=arr[low];
  34. FileInfopivot=arr[low];//把中心轴置于arr[0]
  35. while(low<high)
  36. {
  37. while(low<high&&arr[high].CreationTime<=pivot.CreationTime)
  38. --high;
  39. //将比中心轴记录小的移到低端
  40. Swap(refarr[high],refarr[low]);
  41. while(low<high&&arr[low].CreationTime>=pivot.CreationTime)
  42. ++low;
  43. Swap(refarr[high],refarr[low]);
  44. //将比中心轴记录大的移到高端
  45. }
  46. arr[low]=pivot;//中心轴移到正确位置
  47. returnlow;//返回中心轴位置
  48. }
  49. staticvoidSwap(refFileInfoi,refFileInfoj)
  50. {
  51. FileInfot;
  52. t=i;
  53. i=j;
  54. j=t;
  55. }
  56. ///<summary>
  57. ///快速排序算法
  58. ///</summary>
  59. ///快速排序为不稳定排序,时间复杂度O(nlog2n),为同数量级中最快的排序方法
  60. ///<paramname="arr">划分的数组</param>
  61. ///<paramname="low">数组低端上标</param>
  62. ///<paramname="high">数组高端下标</param>
  63. publicstaticvoidQuickSort(FileInfo[]arr,intlow,inthigh)
  64. {
  65. if(low<=high-1)//当arr[low,high]为空或只一个记录无需排序
  66. {
  67. intpivot=Partition(arr,low,high);
  68. QuickSort(arr,low,pivot-1);
  69. QuickSort(arr,pivot+1,high);
  70. }
  71. }
  72. }

如使用其它排序算法请参考:http://www.yaosansi.com/blog/article.asp?id=980

使用方法:

  1. System.IO.DirectoryInfodir=newDirectoryInfo(currentFolder);
  2. System.IO.FileInfo[]files=dir.GetFiles();
  3. MyQuickSort.QuickSort(files,0,files.Length-1);//按时间排序

使用后:

如果files的长度大于0,那么files[0]为创建时间最新的文件.

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。