sorting an arrayList of objects

A

Adam

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

Willie Ferguson

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)).GetValue(member, Nothing)

Next

Return member

End Function

End Class.

Willie

nntp://news.microsoft.com/microsoft.public.dotnet.framework.aspnet/<[email protected]>

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]
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top