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
 
C

Cor Ligthert

Derek,

Why you want only the arraylist while there are so many other
ilist/icontainer classes that does it direct, I said last time the
datatable, however there is as well the hashtable or maybe even better the
sortedlist.

Otherwise you are doing something what everybody else sees no need to, so
why take effort in that.
However there can be someone who want to help you to make what is done in
another way. However when not don't set here that you did not got an answer.

http://msdn.microsoft.com/library/d...rlrfsystemcollectionssortedlistclasstopic.asp

However I hope this helps anyway?

Cor
 
L

Larry Serflaten

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.
I have read that I can implement icomparable or icomparer in the object
class but can't quite put my finger on how?


Add a button to a new form and paste in the code below, see if that can
get you started on using the IComparer interface....

HTH
LFS


Private Structure MyData
Public Name As String
Public Age As Short

Public Sub New(ByVal Name As String, ByVal Age As Short)
Me.Name = Name
Me.Age = Age
End Sub
End Structure


Private Class NameSorter
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Return New CaseInsensitiveComparer().Compare( _
DirectCast(x, MyData).Name, _
DirectCast(y, MyData).Name)
End Function
End Class

Private Class AgeSorter
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Return DirectCast(x, MyData).Age.CompareTo(DirectCast(y, MyData).Age)
End Function
End Class


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click

Dim text As MyData
Dim data() As MyData
data = New MyData() {New MyData("Joe", 10), New MyData("Sue", 12), _
New MyData("Bob", 16), New MyData("Sam", 8)}


Console.WriteLine(vbCrLf & "Raw data:")
For Each text In data
Console.WriteLine(text.Name & " " & text.Age)
Next

Console.WriteLine(vbCrLf & "By Name:")
Array.Sort(data, New NameSorter)
For Each text In data
Console.WriteLine(text.Name & " " & text.Age)
Next

Console.WriteLine(vbCrLf & "By Age")
Array.Sort(data, New AgeSorter)
For Each text In data
Console.WriteLine(text.Name & " " & text.Age)
Next

End Sub
 
G

Guest

The below code is for an array named Rows of classes where each element of
the array is an object of "myClass" type. Note that "myProperty" is the
"myClass" Property that you want to sort on. SortOrder True is for ascending
and CaseSensitive is true if you want a sort that is case sensitive. Hope
this helps.


Dim mycomparer As mycompare = New mycompare(SortOrder, CaseSensitive)
Array.Sort(Rows, mycomparer)

Public Class mycompare
Implements IComparer
Private m_ascending As Boolean
Private ci As CultureInfo = New CultureInfo("")
Private v_CaseSensitive As Boolean
Public Sub New(ByVal ascending As Boolean, ByVal valuetype As Type, ByVal
CaseSensitive As Boolean)
m_ascending = ascending
v_CaseSensitive = CaseSensitive
End Sub

Private Function Compare(ByVal x As Object, ByVal y As Object) As Integer
_ Implements IComparer.Compare
x = DirectCast(x, myClass).MyProperty
y = DirectCast(y, myClass).MyProperty
If m_ascending Then
If Not v_CaseSensitive Then Return New CaseInsensitiveComparer
_().Compare(x, y) Else Return New Comparer(ci).Compare(x, y)
Else
If Not v_CaseSensitive Then Return New CaseInsensitiveComparer
_().Compare (y, x) Else Return New Comparer(ci).Compare(y, x)
End If

End Class
 
G

Guest

Note that in the previous example I sent you, if X or Y object value is
DBnull, it will fail. I actually have a try/catch around the .compare method
where I catch any x.GetType.Name = "DBNull" and set it to whatever data type
value would be equal to Null such as string would be "". Good Luck
 
D

Derek Martin

Thank you all so much! I will take a peak at all of these wonderful
suggestions as soon as I get unstuffed from the Pre-Pre-Christmas dinner!

Derek
 

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