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

sorting an arrayList of objects

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.