Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

complex List<> sorting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tem - 13 Apr 2008 08:24 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

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.