I have an Interface InterfaceBase, and two classes Type 1 and Type 2 that
implements InterfaceBase
Class Type1 has a string property Title
Class Type2 has a string property Title and an int property Ordering
List<BaseC> list
I would like to sort the list by the value of Ordering (Type 1's Ordering is
always 0) and a secondary order by the string property Title alphabetically.
I tried writing my own Comparision code, it doesn't seem to work
Thanks for your help
Tem
Tem - 13 Apr 2008 08:47 GMT
I would like to know how to write this in LINQ
>I have an Interface InterfaceBase, and two classes Type 1 and Type 2 that
>implements InterfaceBase
[quoted text clipped - 13 lines]
>
> Tem
Marc Gravell - 14 Apr 2008 08:57 GMT
> I would like to know how to write this in LINQ
You don't need LINQ for this; you can sort the existing list using a
Comparer<T>:
List<InterfaceBase> list = new List<InterfaceBase>();
list.Sort((x, y) =>
{
int result = x.Ordering.CompareTo(y.Ordering);
if (result == 0) result = string.Compare(x.Title, y.Title);
return result;
});
or in C# 2 (VS2005):
list.Sort(delegate (InterfaceBase x, InterfaceBase y)
{
int result = x.Ordering.CompareTo(y.Ordering);
if (result == 0) result = string.Compare(x.Title, y.Title);
return result;
});
If you really want to use LINQ, you can - but note that you get a new
list - you don't sort the existing one:
List<InterfaceBase> newList = (
from item in list
orderby item.Ordering, item.Title
select item).ToList();
Marc
Gilles Kohl [MVP] - 13 Apr 2008 13:11 GMT
>I have an Interface InterfaceBase, and two classes Type 1 and Type 2 that
>implements InterfaceBase
>
>Class Type1 has a string property Title
>Class Type2 has a string property Title and an int property Ordering
I assume neither property is part of the interface?
>List<BaseC> list
What is BaseC as opposed to InterfaceBase?
>I would like to sort the list by the value of Ordering (Type 1's Ordering is
>always 0) and a secondary order by the string property Title alphabetically.
>
>I tried writing my own Comparision code, it doesn't seem to work
Could you post some actual code - or maybe a simplified, but
compileable version of what you are trying to do - it is hard to help
without knowing the details or seeing what you tried that didn't work.
Regards,
Gilles.
Tem - 14 Apr 2008 08:40 GMT
Sorry it's a typo it's supposed to be List<InterfaceBase> list
>>I have an Interface InterfaceBase, and two classes Type 1 and Type 2 that
>>implements InterfaceBase
[quoted text clipped - 21 lines]
> Regards,
> Gilles.
Peter Duniho - 13 Apr 2008 18:46 GMT
> [...]
> I would like to sort the list by the value of Ordering (Type 1's
> Ordering is always 0) and a secondary order by the string property Title
> alphabetically.
>
> I tried writing my own Comparision code, it doesn't seem to work
As Gilles suggests, It would be better if you can show what you've already
tried. That has two benefits: first, we can see what particular approach
to comparing and sorting you're trying; second, we can provide an answer
that more closely fits what you've done already, rather than sending you
down some entirely different-but-equivalent path.
The general answer is that you'll need to either create a new interface
that can be used for sorting that encapsulates the properties you want to
sort on (Type1 would always return 0 for the Ordering, of course), or you
need a comparer that understands that you're dealing with two different
classes, checks the actual type of each object and compares them
appropriately.
In the former paradigm, you can simply make the new interface inherit
IComparable and have each class implement their own Compare() method. In
the latter paradigm, you'll probably be writing a standalone IComparer
that does the work, though you could in fact have each class implement
IComparable<BaseC> and do that work.
You mention in a second post that you want to do this in LINQ, but I'm not
sure that really has much to do with this. The hard part is unifying your
types so that they are comparable, and that's going to be pretty much the
same with or without LINQ, I think.
Pete
Tem - 14 Apr 2008 08:43 GMT
I've decided to use another method to store the data.
Thank you guys for your help.
>I have an Interface InterfaceBase, and two classes Type 1 and Type 2 that
>implements InterfaceBase
[quoted text clipped - 13 lines]
>
> Tem