![]() |
Tipp 0118
|
Datum sortieren
|
 |
|
Autor/Einsender: Datum: |
|
Klaus D. Raudszus 23.04.2006 |
|
Entwicklungsumgebung: |
|
VB.Net 2003 |
Framework: |
|
1.1 |
|
|
Ein Tipp zum Sortierer mit der Schnittstelle IComparable. Wahlweise können Datumsangaben auf- bzw. absteigend sortiert werden.
Aber auch eine alphabetische Sortierung ist möglich. Mit System.IComparable.CompareTo wird ein Objektvergleich wahlweise nach Datum
oder nach String durchgeführt. Das Ganze ist handlich zusammengefasst in einer Klasse.
|
|
|
Option Strict On
Public Enum SortBy
ByDate
ByName
End Enum
Public Class clsSort
Implements IComparable
Public ReadOnly Name As String
Public ReadOnly [Date] As DateTime
Protected isAscend As Boolean
Protected Sort As SortBy
Public Sub New(ByVal name As String, ByVal [date] As DateTime, _
ByVal isascend As Boolean, ByVal esortby As SortBy)
Me.Name = name
Me.Date = [date]
Me.isAscend = isascend
Sort = esortby
End Sub
Public Function CompareTo(ByVal obj As Object) As Integer _
Implements System.IComparable.CompareTo
If Not TypeOf obj Is clsSort Then _
Throw New ArgumentException( _
"ObjektVergleich ist fehlgeschlagen")
Dim sorter As clsSort = DirectCast(obj, clsSort)
Select Case Sort
Case SortBy.ByDate
If isAscend Then : Return Me.Date.CompareTo(sorter.Date)
Else : Return -Me.Date.CompareTo(sorter.Date)
End If
Case SortBy.ByName
If isAscend Then : Return Me.Name.CompareTo(sorter.Name)
Else : Return -Me.Name.CompareTo(sorter.Name)
End If
End Select
End Function
End Class
|
|
|
|
|
Windows-Version |
98/SE |
 |
|
ME |
 |
|
NT |
 |
|
2000 |
 |
|
XP |
 |
|
Vista |
 |
|
Win
7 |
 |
|
|
|
Download (8,7
kB)
|
Downloads bisher: [ 494 ]
|
|
|