471,627 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

IComparer Question

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
10 2693
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
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
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
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
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
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
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
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
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
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.

Similar topics

3 posts views Thread by Cybertof | last post: by
1 post views Thread by INeedADip | last post: by
4 posts views Thread by christriddle | last post: by
1 post views Thread by Brett Romero | last post: by
10 posts views Thread by Tony | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.