473,385 Members | 1,396 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,385 software developers and data experts.

sorting an arrayList of objects

Hi,

I've got a class which contains a number of properties which in turn are
read from a database. For example:

class TData
public property firstName
<get and set here>
public property surname
<get and set here>

etc. etc.
end class
I use an iComparer to enable sorting of the arrayList, however currently I
need to create a new class (implementing iComparer) for each property I want
to sort by.

Each iComparer class is virtually the same, with the exception of the line:

return directCast(x, TItems).firstname.compareTo(directCast(y,
TItems).firstname)

(where x and y are objects passed into the "compare" function)

I'm looking for a way of creating a single iComparer class which to which I
can pass a variable name, and sort by that. If VB.net had a JavaScript
"Eval"-type function, I could do it with a:

eval ("directCast(x, TItems)."+propName+".compareTo(directCast(y,
TItems)."+propName+")")

Any ideas? (He says, wondering if he's made himself clear!!).

Thanks,
A.


Nov 19 '05 #1
2 1372
Adam,

You can use reflection and pass the property name as a string. I use the following class to do this.

Public Class PropertyComparer

Implements IComparer

Private _propertyName() As String

Public Sub New(ByVal PropertyName As String)

_propertyName = New String() {PropertyName}

End Sub

Public Sub New(ByVal PropertyName() As String)

_propertyName = PropertyName

End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare

Dim i As Integer

Static j As Integer

Try

i = Comparer.Default.Compare(GetPropertyValue(x, _propertyName(j)), GetPropertyValue(y, _propertyName(j)))

If i = 0 Then

j += 1

If j < _propertyName.Length Then _

i = Compare(x, y)

j -= 1

End If

Catch e As System.Exception

i = 0

End Try

Return i

End Function

Private Function GetPropertyValue(ByVal o As Object, ByVal PropertyName As String) As Object

Dim memberArray() As String = PropertyName.Split(New Char() {"."})

Dim member As Object = o

Dim i As Integer

For i = 0 To memberArray.Length - 1

member = member.GetType.GetProperty(memberArray(i)).GetValu e(member, Nothing)

Next

Return member

End Function

End Class.

Willie

nntp://news.microsoft.com/microsoft.public.dotnet.framework.aspnet/<dx********************@fe2.news.blueyonder.co.u k>

Hi,

I've got a class which contains a number of properties which in turn are
read from a database. For example:

class TData
public property firstName
<get and set here>
public property surname
<get and set here>

etc. etc.
end class
I use an iComparer to enable sorting of the arrayList, however currently I
need to create a new class (implementing iComparer) for each property I want
to sort by.

Each iComparer class is virtually the same, with the exception of the line:

return directCast(x, TItems).firstname.compareTo(directCast(y,
TItems).firstname)

(where x and y are objects passed into the "compare" function)

I'm looking for a way of creating a single iComparer class which to which I
can pass a variable name, and sort by that. If VB.net had a JavaScript
"Eval"-type function, I could do it with a:

eval ("directCast(x, TItems)."+propName+".compareTo(directCast(y,
TItems)."+propName+")")

Any ideas? (He says, wondering if he's made himself clear!!).

Thanks,
A.


[microsoft.public.dotnet.framework.aspnet]
Nov 19 '05 #2
Willie,

That is *exactly* what I was after! Thank you so much!!

A.
"Willie Ferguson" <wi*************@gmail.com> wrote in message
news:O0**************@tk2msftngp13.phx.gbl...
Adam,

You can use reflection and pass the property name as a string. I use the
following class to do this.

Public Class PropertyComparer

Implements IComparer

Private _propertyName() As String

Public Sub New(ByVal PropertyName As String)

_propertyName = New String() {PropertyName}

End Sub

Public Sub New(ByVal PropertyName() As String)

_propertyName = PropertyName

End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collections.IComparer.Compare

Dim i As Integer

Static j As Integer

Try

i = Comparer.Default.Compare(GetPropertyValue(x,
_propertyName(j)), GetPropertyValue(y, _propertyName(j)))

If i = 0 Then

j += 1

If j < _propertyName.Length Then _

i = Compare(x, y)

j -= 1

End If

Catch e As System.Exception

i = 0

End Try

Return i

End Function

Private Function GetPropertyValue(ByVal o As Object, ByVal
PropertyName As String) As Object

Dim memberArray() As String = PropertyName.Split(New Char() {"."})

Dim member As Object = o

Dim i As Integer

For i = 0 To memberArray.Length - 1

member =
member.GetType.GetProperty(memberArray(i)).GetValu e(member, Nothing)

Next

Return member

End Function

End Class.

Willie
nntp://news.microsoft.com/microsoft.public.dotnet.framework.aspnet/<dx********************@fe2.news.blueyonder.co.u k>

Hi,

I've got a class which contains a number of properties which in turn are
read from a database. For example:

class TData
public property firstName
<get and set here>
public property surname
<get and set here>

etc. etc.
end class
I use an iComparer to enable sorting of the arrayList, however currently I
need to create a new class (implementing iComparer) for each property I
want
to sort by.

Each iComparer class is virtually the same, with the exception of the
line:

return directCast(x, TItems).firstname.compareTo(directCast(y,
TItems).firstname)

(where x and y are objects passed into the "compare" function)

I'm looking for a way of creating a single iComparer class which to which
I
can pass a variable name, and sort by that. If VB.net had a JavaScript
"Eval"-type function, I could do it with a:

eval ("directCast(x, TItems)."+propName+".compareTo(directCast(y,
TItems)."+propName+")")

Any ideas? (He says, wondering if he's made himself clear!!).

Thanks,
A.


[microsoft.public.dotnet.framework.aspnet]

Nov 19 '05 #3

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.