原文C#关键代码部分
public partial class SortFileList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo dirinfo = new DirectoryInfo(MapPath("~"));
FileInfo[] sortList = dirinfo.GetFiles();
Array.Sort(sortList, new MyDateSorter());
foreach (FileInfo item in sortList)
{
Response.Write(item.FullName + "<br" + " />");
}
}
}
public class MyDateSorter : IComparer
{
#region IComparer Members
public int Compare(object x, object y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null)
{
return -1;
}
if (y == null)
{
return 1;
}
FileInfo xInfo = (FileInfo)x;
FileInfo yInfo = (FileInfo)y;
//依名稱排序
return xInfo.FullName.CompareTo(yInfo.FullName);//遞增
//return yInfo.FullName.CompareTo(xInfo.FullName);//遞減
//依修改日期排序
//return xInfo.LastWriteTime.CompareTo(yInfo.LastWriteTime);//遞增
//return yInfo.LastWriteTime.CompareTo(xInfo.LastWriteTime);//遞減
}
#endregion
}
我使用的改为VB.NET版本:
Dim dir As New DirectoryInfo(SavePath)
Dim Files() As FileInfo
Files = dir.GetFiles("*.sql")
Array.Sort(Files, New MyDateSorter())
Dim f As FileInfo
For Each f In Files
Html_Rec = "<tr align=""center"" >"
Html_Rec &= String.Format("<td>{0}</td><td>{1}</td><td>{2}</td><td><input name=""addkc"" type=""button"" class=""an"" id=""restore"" οnclick=""restore('{3}','{4}')"" value=""恢 复"" /> <input name=""addkc"" type=""button"" class=""an"" id=""del"" οnclick=""del('{5}','{6}')"" value=""删 除"" /></td>", i, f.CreationTime, f.Name, f.CreationTime, f.Name, f.CreationTime, f.Name)
Html_Rec &= " </tr>"
Html.Append(Html_Rec)
i += 1
Next
Html.Append("</table>")
排序类:
Public Class MyDateSorter
Implements IComparer
Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare
If x Is Nothing AndAlso y Is Nothing Then
Return 0
End If
If x Is Nothing Then
Return -1
End If
If y Is Nothing Then
Return 1
End If
Dim xInfo As FileInfo = DirectCast(x, FileInfo)
Dim yInfo As FileInfo = DirectCast(y, FileInfo)
'依名稱排序
'Return xInfo.FullName.CompareTo(yInfo.FullName)
'遞增
'return yInfo.FullName.CompareTo(xInfo.FullName);//遞減
'依修改日期排序
Return xInfo.LastWriteTime.CompareTo(yInfo.LastWriteTime) '遞增
'return yInfo.LastWriteTime.CompareTo(xInfo.LastWriteTime) '遞減
End Function
End Class