|
> 可以对字母字符和数字进行排序。但是,使用 String.Compare 不能对特定数据类型进行正确排序,例如日期和时间信息。因此,System.DateTime 结构具有一个与 String 类相同作用的 Compare 方法。这个方法可以用来基于时间顺序执行相同类型的排序。在本部分中,您只需修改 Compare 方法就可以正确排序日期。
添加以下代码以替换为 Compare 方法(该方法是前面示例中定义的 ListViewItemComparer 类的方法)而定义的代码。
''''Visual Basic
Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collections.IComparer.Compare
Dim returnVal As Integer
'''' Determine whether the type being compared is a date type.
Try
'''' Parse the two objects passed as a parameter as a DateTime.
Dim firstDate As System.DateTime = DateTime.Parse(CType(x, _
ListViewItem).SubItems(col).Text)
Dim secondDate As System.DateTime = DateTime.Parse(CType(y, _
ListViewItem).SubItems(col).Text)
'''' Compare the two dates.
returnVal = DateTime.Compare(firstDate, secondDate)
'''' If neither compared object has a valid date format,
'''' compare as a string.
Catch
'''' Compare the two items as a string.
returnVal = [String].Compare(CType(x, _
ListViewItem).SubItems(col).Text, CType(y,ListViewItem).SubItems(col).Text)
End Try
'''' Determine whether the sort order is descending.
If order = SortOrder.Descending Then
'''' Invert the value returned by String.Compare.
returnVal *= -1
& 上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页 [系统软件]windows下Apache+php+mysql的安装与配置图解 [Web开发]VB的窗体布局窗口为什么找不到窗体的Form1小图标 [电脑技术]UNformAT恢复格式化命令介绍 [操作系统]在Windows中玩转Linux操作系统 [操作系统]死马还当活马医:6种方法挽救Windows系统 [聊天工具]四大更新 Windows Live Msn 8.1评测 [聊天工具]Windows Live Messenger最新0683版亮相_联络工具_… [聊天工具]Windows Live Mail招人爱的N个理由_联络工具_Wind… [聊天工具]Windows Live Mail Desktop多图欣赏_联络工具_Win… [聊天工具]OE老了 微软开发新邮件客户端取而代之_联络工具
|