[String].Compare(xTEXT,Ytext) 中括号什么意思?
这些天在学习Listview控件,其中遇到一个问题是 listview.view=Details 想实现双击列标题,实现ITEMS排序
在网上找到以下代码
Public Class ColumnSort : Implements IComparer
Private columnNum As Integer = 0
Public bAscending As Boolean = True
Public Sub New(ByVal column_to_sort As Integer)
columnNum = column_to_sort
End Sub
Private Function CompareInt(ByVal x As Integer, ByVal y As Integer) As Integer
If x > y Then
Return 1
ElseIf x < y Then
Return -1
Else
Return 0
End If
End Function
Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
Dim listItemA As System.Windows.Forms.ListViewItem = DirectCast(a, System.Windows.Forms.ListViewItem)
Dim listItemB As System.Windows.Forms.ListViewItem = DirectCast(b, System.Windows.Forms.ListViewItem)
Dim xText As String = listItemA.SubItems(columnNum).Text
Dim yText As String = listItemB.SubItems(columnNum).Text
Dim xInt As Integer, yInt As Integer
If bAscending Then
If Integer.TryParse(xText, xInt) AndAlso Integer.TryParse(yText, yInt) Then
Return CompareInt(xInt, yInt)
Else
Return [String].Compare(xText, yText)
End If
Else
If Integer.TryParse(xText, xInt) AndAlso Integer.TryParse(yText, yInt) Then
Return -1 * CompareInt(xInt, yInt)
Else
Return -1 * [String].Compare(xText, yText)
End If
End If
End Function
End Class
实在是弄不清楚[String].Compare(xText,yText)的意思;
我知道构造函数[new] 中括号是为了和NEW关键字做区分,这里是为了什么呢?
另外这里的Compare是调用自身吗?
VB.NET中括号
[解决办法]
String实现了IComparer接口,提供了Compare方法。用来比较两个字符串的文本序哪一个更大,哪一个更小,以便排序。
参考:http://msdn.microsoft.com/zh-cn/library/system.collections.icomparer.aspx