473,421 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Sorting an Arraylist of Objects

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
3 1117
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick | last post by:
Hi ! I have Objects in my ArrayList These Objects contain a String called "Name". And i want that ArrayList to sort it's objects using the Name element of each object and not something else of...
1
by: Daniel | last post by:
does C# have any collection objects that support sort functionality so that I dont have to write my own sorting algorithm?
7
by: ryba | last post by:
Hello I'm sorry for mistakes - my English isn't very well. I've got the problem with sorting objects in ArrayList. If I put there only strings, Sort method works great, but it doesnt work when I...
6
by: Bryon | last post by:
I need to sort an ArrayList of objects. I am unable to find a method for this. DO I need to role my own sorting? Or is there something like the qsort() function in C of old??? Thanks
2
by: Rob G | last post by:
Hello, I have a basic understanding of Objects, inheritance, polymorhpism, etc. I am new to VB.NET and I am having a problem figuring this out. I want to sort a CheckBoxList. Using the...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
2
by: Rob Meade | last post by:
Dear all, I have a class which contains an arraylist populated with other objects, for example: PrescriptionQueue - containing multiple instances of Prescription I have the need on my web...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
0
by: planb | last post by:
Hello, I am working with a class with a basetype of NameObjectCollection, which has as a member an object array public object Objects {get {return BaseGetAllValues();}} which I want to sort...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.