Add a button and listbox
Insert the following code into form1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a() As String = {"4", "5", "10", "12", "14", "15", "7", "8", "9", "3", "18", "16", "15", "100", "13", "55"}
Dim sc As New NumericComparer(True)
Array.Sort(a, sc)
ListView1.Items.Clear()
For i As Integer = 0 To a.Length - 1
ListBox1.Items.Add(a(i))
Next
End Sub
End Class
Implementing the IComparer.Compare
Add a new class insert following code.
Public Class NumericComparer
Implements IComparer
Dim mText As Boolean = False
Public Sub New(ByRef sorttype As NumericSort)
mText = sorttype
End Sub
Public Enum NumericSort
Textual = True
Numerical = False
End Enum
' Compare the two employees.
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
If mText = False Then
Dim txtx As Double = CDbl(x)
Dim txty As Double = CDbl(y)
Select Case True
Case txtx < txty : Return -1
Case txtx = txty : Return 0
Case txtx > txty : Return 1
End Select
Else
Dim txtx As String = CStr(x)
Dim txty As String = CStr(y)
Return String.Compare(txtx, txty)
End If
End Function
End Class





MultiQuote


|