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

IComparer Question

P: n/a
I am trying to use a generic (reflection) IComparer class to sort a
generic list but I get the error:
Unable to cast object of type 'GenericComparer' to type
'System.Collections.Generic.Icomparer `1[AccountDB.Queue]'

My code looks like the following:

List<AccountDB.Queue> oList = getAllQueuesFunction();
List.Sort(New GenericComparer("QueueName")); <-- Error

My class looks like:

public class GenericComparer : IComparer
{
string propertyName;

public GenericComparer(string propertyName)
{
this.propertyName = propertyName;
}

public int Compare(object x, object y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);

// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);

// now makes the comparsion
return ((IComparable)valueOfX).CompareTo(valueOfY);
}
}

Hopefully you can see what I am trying to accomplish. Am I approaching
this the wrong way? Any suggestions or Comments?

Jan 6 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
INeedADip <in*******@gmail.com> wrote:
I am trying to use a generic (reflection) IComparer class to sort a
generic list but I get the error:
Unable to cast object of type 'GenericComparer' to type
'System.Collections.Generic.Icomparer `1[AccountDB.Queue]'

My code looks like the following:

List<AccountDB.Queue> oList = getAllQueuesFunction();
List.Sort(New GenericComparer("QueueName")); <-- Error


You've implemented System.Collections.IComparer. You need to implement
System.Collections.Generic.IComparer<T>.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 6 '06 #2

P: n/a
Hi,

You get this error because your GenericComparer implements IComparer not
IComparer<T>. Despite the same name both interfaces are different.

If you look at the Sort method of List<T> generic you'll see that it is
declared as

public void Sort (
IComparer<T> comparer
)You need to declare you comparer aspublic class GenericComparer :
IComparer<[the type of the elements in the list]>or you can create generic
comparer and specify the type of the elements upon creation.-- HTHStoitcho
Goutsev (100)"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I am trying to use a generic (reflection) IComparer class to sort a
generic list but I get the error:
Unable to cast object of type 'GenericComparer' to type
'System.Collections.Generic.Icomparer `1[AccountDB.Queue]'

My code looks like the following:

List<AccountDB.Queue> oList = getAllQueuesFunction();
List.Sort(New GenericComparer("QueueName")); <-- Error

My class looks like:

public class GenericComparer : IComparer
{
string propertyName;

public GenericComparer(string propertyName)
{
this.propertyName = propertyName;
}

public int Compare(object x, object y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);

// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);

// now makes the comparsion
return ((IComparable)valueOfX).CompareTo(valueOfY);
}
}

Hopefully you can see what I am trying to accomplish. Am I approaching
this the wrong way? Any suggestions or Comments?

Jan 6 '06 #3

P: n/a
Any ideas on how I should approach this?

I thought maybe IComparer<object> but I can't quite get it to work..

Jan 7 '06 #4

P: n/a
INeedADip <in*******@gmail.com> wrote:
Any ideas on how I should approach this?

I thought maybe IComparer<object> but I can't quite get it to work..


Make your comparer generic as well. Something like (untested):

public class GenericComparer<T> : IComparer<T>
{
string propertyName;

public GenericComparer(string propertyName)
{
this.propertyName = propertyName;
}

public int Compare(T x, T y)
{
// gets the value of the x property
PropertyInfo property = x.GetType()
.GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);

// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);

// now makes the comparsion
return ((IComparable)valueOfX).CompareTo(valueOfY);
}
}

Note that I'm still using GetType() in the above rather than typeof(T)
in case the properties aren't provided by T itself, but by a class
derived from T.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 7 '06 #5

P: n/a
Instead of creating an implementation of IComparer<T>, you can just use
anonymous delegates as well (unless you need to use this implementation in a
good number of places).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I am trying to use a generic (reflection) IComparer class to sort a
generic list but I get the error:
Unable to cast object of type 'GenericComparer' to type
'System.Collections.Generic.Icomparer `1[AccountDB.Queue]'

My code looks like the following:

List<AccountDB.Queue> oList = getAllQueuesFunction();
List.Sort(New GenericComparer("QueueName")); <-- Error

My class looks like:

public class GenericComparer : IComparer
{
string propertyName;

public GenericComparer(string propertyName)
{
this.propertyName = propertyName;
}

public int Compare(object x, object y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);

// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);

// now makes the comparsion
return ((IComparable)valueOfX).CompareTo(valueOfY);
}
}

Hopefully you can see what I am trying to accomplish. Am I approaching
this the wrong way? Any suggestions or Comments?

Jan 7 '06 #6

P: n/a
Jon Skeet - Thank you very much for the help.
I'm sure there are better ways of doing this, but this seems like a pretty
good way to sort my objects (without making a bunch of IComparer<T>
classes).
I wanted to post the final solution here incase anyone else is looking for
something similar, it sure would have saved me a lot of time.

This will allow you to sort on a List<T> of custom classes with simple value
type properties:

public enum GenericComparerSortDirection { Asc, Desc }

public class GenericComparer<T> : IComparer<T>
{

private string propertyName;
private GenericComparerSortDirection theDirection;

public GenericComparer(string propertyName,
GenericComparerSortDirection eSortDirection)
{
this.propertyName = propertyName;
this.theDirection = eSortDirection;
}

public int Compare(T x, T y)
{
// gets the value of the x property
PropertyInfo property = x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);

// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);

// now make the comparsion
if (this.theDirection == GenericComparerSortDirection.Asc)
return ((IComparable)valueOfX).CompareTo(valueOfY);
else
return ((IComparable)valueOfY).CompareTo(valueOfX);
}
}

Usage:

List<MyClass> lstSorted = getABunchOfObjects();
lstSorted.Sort(new GenericComparer<MyClass>("PropertyName",
GenericComparerSortDirection.Asc));

--------
Someone with a blog should post this for others.
Jan 8 '06 #7

P: n/a
Thank you very much for the help.
I'm sure there are better ways of doing this, but this seems like a
pretty good way to sort my objects (without making a bunch of
IComparer<T> classes).
I wanted to post the final solution here incase anyone else is looking
for something similar, it sure would have saved me a lot of time.

This will allow you to sort on a List<T> of custom classes with simple
value type properties:

public enum GenericComparerSortDirection { Asc, Desc }

public class GenericComparer<T> : IComparer<T>
{

private string propertyName;
private GenericComparerSortDirection theDirection;

public GenericComparer(string propertyName,
GenericComparerSortDirection eSortDirection)
{
this.propertyName = propertyName;
this.theDirection = eSortDirection;
}

public int Compare(T x, T y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);

// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);

// now make the comparsion
if (this.theDirection == GenericComparerSortDirection.Asc)
return ((IComparable)valueOfX).CompareTo(valueOfY);
else
return ((IComparable)valueOfY).CompareTo(valueOfX);
}
}

Usage:

List<MyClass> lstSorted = getABunchOfObjects();
lstSorted.Sort(new GenericComparer<MyClass>("PropertyName",
GenericComparerSortDirection.Asc));

--------
Someone with a blog should post this for others.

Jan 8 '06 #8

P: n/a
Thank you very much for the help.
I'm sure there are better ways of doing this, but this seems like a
pretty good way to sort my objects (without making a bunch of
IComparer<T> classes).
I wanted to post the final solution here incase anyone else is looking
for something similar, it sure would have saved me a lot of time.

This will allow you to sort on a List<T> of custom classes with simple
value type properties:

public enum GenericComparerSortDirection { Asc, Desc }

public class GenericComparer<T> : IComparer<T>
{

private string propertyName;
private GenericComparerSortDirection theDirection;

public GenericComparer(string propertyName,
GenericComparerSortDirection eSortDirection)
{
this.propertyName = propertyName;
this.theDirection = eSortDirection;
}

public int Compare(T x, T y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);

// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);

// now make the comparsion
if (this.theDirection == GenericComparerSortDirection.Asc)
return ((IComparable)valueOfX).CompareTo(valueOfY);
else
return ((IComparable)valueOfY).CompareTo(valueOfX);
}
}

Usage:

List<MyClass> lstSorted = getABunchOfObjects();
lstSorted.Sort(new GenericComparer<MyClass>("PropertyName",
GenericComparerSortDirection.Asc));

--------
Someone with a blog should post this for others.

Jan 8 '06 #9

P: n/a
I did think about that, but I use it so often, this was a better way...

Jon Skeet - Thank you very much for the help.
I'm sure there are better ways of doing this, but this seems like a
pretty good way to sort my objects (without making a bunch of
IComparer<T> classes).
I wanted to post the final solution here incase anyone else is looking
for something similar, it sure would have saved me a lot of time.

This will allow you to sort on a List<T> of custom classes with simple
value type properties:

public enum GenericComparerSortDirection { Asc, Desc }

public class GenericComparer<T> : IComparer<T>
{

private string propertyName;
private GenericComparerSortDirection theDirection;

public GenericComparer(string propertyName,
GenericComparerSortDirection eSortDirection)
{
this.propertyName = propertyName;
this.theDirection = eSortDirection;
}

public int Compare(T x, T y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);

// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);

// now make the comparsion
if (this.theDirection == GenericComparerSortDirection.Asc)
return ((IComparable)valueOfX).CompareTo(valueOfY);
else
return ((IComparable)valueOfY).CompareTo(valueOfX);
}
}

Usage:

List<MyClass> lstSorted = getABunchOfObjects();
lstSorted.Sort(new GenericComparer<MyClass>("PropertyName",
GenericComparerSortDirection.Asc));

--------
Someone with a blog should post this for others.

Jan 9 '06 #10

P: n/a
For reference or any more discussion:

http://ineedadip.blogspot.com/2006/0...icomparer.html

Feb 1 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.