タイトル | : ソートのパターンを変更するコード |
記事No | : 779 |
投稿日 | : 2004/03/18(Thu) 17:53 |
投稿者 | : 特攻隊長まるるう |
興味があったのでちょっと触らせてもらいました。 大変勉強になりました。 タートルさん、魔界の仮面弁士さん、ありがとうございます。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim a(2) As MyUDT MyUDT.Pattern = 1 a(0) = New MyUDT("CCC", "BBB") a(1) = New MyUDT("BBB", "AAA") a(2) = New MyUDT("A", "GGG") Array.Sort(a) MyUDT.Pattern = 2 Array.Sort(a) End Sub
Protected Friend Structure MyUDT : Implements System.IComparable Friend Code As String, Name As String Shared Pattern As Integer Friend Sub New(ByVal Code As String, ByVal Name As String) Me.Code = Code Me.Name = Name End Sub Private Overloads Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Dim cmp As Integer Try Dim X As MyUDT = DirectCast(obj, MyUDT) Select Case Me.Pattern Case 1 cmp = Me.Code.CompareTo(X.Code) If cmp = 0 Then cmp = Me.Name.CompareTo(X.Name) End If Return cmp Case 2 cmp = Me.Code.CompareTo(X.Name) If cmp = 0 Then cmp = Me.Name.CompareTo(X.Code) End If Return cmp Case Else cmp = Me.Code.CompareTo(X.Code) If cmp = 0 Then cmp = Me.Name.CompareTo(X.Name) End If Return cmp End Select Catch Return 0 End Try End Function
End Structure
|