Sorting an Arraylist of Objects

D

Derek Martin

Hi there, I've asked before but never got an answer that could get me
completely to where I was heading. I have an arraylist that contains
defined objects and I would like to sort this arraylist based on the values
of one of the object's members.

Here is a bit of code to illustrate my setup:

Object construction and insertion:
Dim entity As New entityobjects(entitynamenodelist(i).InnerXml,
typenodelist(i).InnerXml, ...)
entityarraylist.add(entity)
----------------------------------------------------------------------------------------------
Object definition class:
Public Class entityobjects
Protected m_entityid As String
Protected m_entityname As String
Protected m_type As String
...

Public Sub New(ByVal entityname As String, ByVal type As String ...)
Me.entityname = entityname
Me.type = type
...
End Sub
----------------------------------------------------------------------------------------------

Now, I would like to list out the objects sorted by entityname:

'What can I put here to sort these???

for each entity as object in entityarraylist
richtextbox1.appendtext(entity.toString()) 'toString is overridden here
in the object class
next
----------------------------------------------------------------------------------------------

I have read that I can implement icomparable or icomparer in the object
class but can't quite put my finger on how?

Thank you!
Derek
 
M

Mythran

Derek Martin said:
Hi there, I've asked before but never got an answer that could get me
completely to where I was heading. I have an arraylist that contains
defined objects and I would like to sort this arraylist based on the
values of one of the object's members.

Hope the following Console application example helps :)


Public Module Main
<STAThread()> _
Public Sub Main()
Dim list As ArrayList = New ArrayList()
list.Add(New Entity("abcdfz"))
list.Add(New Entity("bacdfz"))
list.Add(New Entity("zfdcba"))
list.Add(New Entity("fdadsf"))
list.Add(New Entity("akjjlk"))
list.Add(New Entity("ieiojd"))
list.Add(New Entity("adfklasjfdlkajs"))

list.Sort()

For Each obj As Entity In list
Console.WriteLine(obj.EntityId)
Next
End Sub

End Module

Public Class Entity
Implements IComparable

Private mEntityId As String

Public Property EntityId() As String
Get
Return mEntityId
End Get
Set
mEntityId = Value
End Set
End Property

Public Sub New(ByVal EntityId As String)
mEntityId = EntityId
End Sub

Public Function CompareTo(ByVal obj As Object) As Integer Implements
System.IComparable.CompareTo
If TypeOf obj Is Entity
Dim tmp As Entity = DirectCast(obj, Entity)
Return mEntityId.CompareTo(tmp.EntityId)
End If

Throw New ArgumentException("The argument is not an Entity.")
End Function
End Class
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top