markzzz
markzzz

Reputation: 47985

How can I order a List according to my own rules?

I have this LINQ query :

IEnumerable<MyScheda> schede = from MyScheda scheda in new MySchede()
                               select scheda;

and I'd like to order this list according to the field scheda.Note, which can contains Saturday, Monday, Sunday, and so on.

So, how you can wish, I'd like to order them as the usual order or the day. How can I do it?

Upvotes: 0

Views: 276

Answers (2)

Omaha
Omaha

Reputation: 2292

You can request an ordering by using orderby:

List<String> names=new List<String>();

...

foreach(String name in from String s in names orderby s.Length select s)
{
...
}

In your case, I believe you'll want to use:

orderby scheda.Note

This assumes, of course, that the type of Note can be compared for ordering. If it cannot be compared directly, you will need to write a some code that converts a "Note-type" to a sortable type, and then use that rather than Note directly, like this:

int NoteIndexer(NoteType n);

IEnumerable<MyScheda> schede = from MyScheda scheda in new MySchede() 
    orderby NoteIndexer(scheda.Note)
    select scheda;

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125640

You can use IEnumerable.OrderBy extension method with your own comparer:

IEnumerable<MyScheda> schede = (from MyScheda scheda in new MySchede()
                               select scheda).OrderBy(s => s.Note, new DaysOfWeekComparer());

of course you have to implement DaysOfWeekComparer() before.

Upvotes: 1

Related Questions