How to sort a List (Of T)

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
 
L

Lloyd Sheen

Assido said:
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

If you are using VS 2008 and dot.net 3.5 you can use Linq to sort the list.

You would do something like:

oSection.Keys = (from k in oSection.Keys select k order by k.Name).ToList

LS
 
A

Assido

Hello Lloyd Sheen,

thanks for your answer, but i only have VS 2005. :-(

Any other ideas are welcome
 
G

Göran Andersson

Assido said:
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?

8<


' // 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

You just have to supply the Sort method with a Comparison:

oSection.Keys.Sort(delegate(Key x, Key y){ return string.Compare(x.Name,
y.Name); })
 
A

Assido

Hello guys,

thanks for your help!

Steve's example worked perfect for me!

Thnaks a lot for your efforts!
 
G

Göran Andersson

Steve said:
Not sure if that C# code will play in VB. :)

I found this way to be reasonable:

In class Key, add a shared function:

Public Shared Function CompKeysByName(ByVal X As Key, ByVal Y As Key) As Integer
Return X.Name.CompareTo(Y.Name)
End Function

Then for the sort, do

oSection.Keys.Sort(AddressOf Key.CompKeysByName)

Yes, you are right, I didn't check the language too carefully before
posting. That would (almost) be the VB version of my code. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top