By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,956 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,956 IT Pros & Developers. It's quick & easy.

Sorting an Arraylist of Objects

P: n/a
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

--
Derek Martin
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Derek:

I have an article here that may be of some help:
http://odetocode.com/Articles/203.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 23 Dec 2004 09:41:39 -0600, "Derek Martin"
<dm*****@DONTSPAMMEokstateDOT.edu> wrote:
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


Nov 19 '05 #2

P: n/a
Thanks Scott, I will investigate!

Derek

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:nl********************************@4ax.com...
Hi Derek:

I have an article here that may be of some help:
http://odetocode.com/Articles/203.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 23 Dec 2004 09:41:39 -0600, "Derek Martin"
<dm*****@DONTSPAMMEokstateDOT.edu> wrote:
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

Nov 19 '05 #3

P: n/a

"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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
Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.