By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 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
593074
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
This should get you started using the IComparer Interface
http://www.asp.net/TimeTrackerStarte...tion.vb&rows=1
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:uQ**************@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.

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
593074

Nov 21 '05 #2

P: n/a
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/de...classtopic.asp

However I hope this helps anyway?

Cor

"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> schreef in bericht
news:uQ**************@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.

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
593074

Nov 21 '05 #3

P: n/a

"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. <...> 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

Nov 21 '05 #4

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

"Derek Martin" 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
--
Derek Martin
593074

Nov 21 '05 #5

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

"Derek Martin" 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
--
Derek Martin
593074

Nov 21 '05 #6

P: n/a
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> schrieb:
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.


CompareOperator
<URL:http://dotnet.mvps.org/dotnet/samples/codingtechnique/CompareOperator.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #7

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

"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:uQ**************@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.

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
593074

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.