A
Assido
Hello @All,
here is my problem ( code below):
I have a List (of T) containing object s(Key-Object) with 2 properties.
- Name
- Value
An other Object (Section-Calss) provides the List (of Key) as a public member.
So far so good! Now i want to sort the List(of Key) in alphabetical order by
the KeyName.
Can someone tell me how to do it?
------------------------------------------
Code
------------------------------------------
Public Class Form1
Dim oSection As New Section
Private Sub btnGenerateKeys_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnGenerateKeys.Click
' // Call the procedure to fill the oSection.Keys-List with some
objects
GenerateSomeKeys()
' // Fill listbox with the keynames
For Each oKey As Key In oSection.Keys
lsbUnsorted.Items.Add(oKey.Name)
Next
End Sub
' // Generate some lsitentries
Private Sub GenerateSomeKeys()
oSection.Keys.Add(New Key("b", "b"))
oSection.Keys.Add(New Key("c", "c"))
oSection.Keys.Add(New Key("f", "f"))
oSection.Keys.Add(New Key("z", "z"))
oSection.Keys.Add(New Key("a", "a"))
End Sub
Private Sub btnSortList_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSortList.Click
' // Call the sortprocedure
SortListByName()
End Sub
' // Here is the Problem !!! How to do it?
' // Sort listentries by Key.Name
Private Sub SortListByName()
oSection.Keys.Sort()
' // Fill the sorted keynames in the listbox
For Each oKey As Key In oSection.Keys
lsbUnsorted.Items.Add(oKey.Name)
Next
End Sub
End Class
' ###############################
' // The Section-Class
' ###############################
Public Class Section
Public Keys As New List(Of Key)
Sub New()
End Sub
End Class
' ###############################
' // The Key-Class
' ###############################
Public Class Key
Private mName As String
Private mValue As String
Sub New()
End Sub
Sub New(ByVal Name As String, ByVal Value As String)
mName = Name
mValue = Value
End Sub
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property
Public Property Value() As String
Get
Return mValue
End Get
Set(ByVal value As String)
mValue = value
End Set
End Property
End Class
here is my problem ( code below):
I have a List (of T) containing object s(Key-Object) with 2 properties.
- Name
- Value
An other Object (Section-Calss) provides the List (of Key) as a public member.
So far so good! Now i want to sort the List(of Key) in alphabetical order by
the KeyName.
Can someone tell me how to do it?
------------------------------------------
Code
------------------------------------------
Public Class Form1
Dim oSection As New Section
Private Sub btnGenerateKeys_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnGenerateKeys.Click
' // Call the procedure to fill the oSection.Keys-List with some
objects
GenerateSomeKeys()
' // Fill listbox with the keynames
For Each oKey As Key In oSection.Keys
lsbUnsorted.Items.Add(oKey.Name)
Next
End Sub
' // Generate some lsitentries
Private Sub GenerateSomeKeys()
oSection.Keys.Add(New Key("b", "b"))
oSection.Keys.Add(New Key("c", "c"))
oSection.Keys.Add(New Key("f", "f"))
oSection.Keys.Add(New Key("z", "z"))
oSection.Keys.Add(New Key("a", "a"))
End Sub
Private Sub btnSortList_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSortList.Click
' // Call the sortprocedure
SortListByName()
End Sub
' // Here is the Problem !!! How to do it?
' // Sort listentries by Key.Name
Private Sub SortListByName()
oSection.Keys.Sort()
' // Fill the sorted keynames in the listbox
For Each oKey As Key In oSection.Keys
lsbUnsorted.Items.Add(oKey.Name)
Next
End Sub
End Class
' ###############################
' // The Section-Class
' ###############################
Public Class Section
Public Keys As New List(Of Key)
Sub New()
End Sub
End Class
' ###############################
' // The Key-Class
' ###############################
Public Class Key
Private mName As String
Private mValue As String
Sub New()
End Sub
Sub New(ByVal Name As String, ByVal Value As String)
mName = Name
mValue = Value
End Sub
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property
Public Property Value() As String
Get
Return mValue
End Get
Set(ByVal value As String)
mValue = value
End Set
End Property
End Class