tags:

views:

122

answers:

2

I have a IEnumerable<T> collection with Name and FullName as items in it. There are around 5000 items in it.

I want to display the FullNames sorted by its lenght, so first the longest name to the shortest name displays. How can I do it in most optimized manner?

+1  A: 

It would help to have some more information about exactly what your data structures look like. But I think that this LINQ query should get you started.

var orderedItems = from name in nameList
                   order by name.FullName.Length descending
                   select name;

Here is a whole set of LINQ Order By examples.

spoon16
Actually I receive a 'var nameList' from the user which supports IEnumerable<T>. The nameList contains data in the following format:Name="Jon" FullName="Jon Terry"Name="Sil" FullName="Sil Marchiu"and so on..
Viks
ok, so the query I have given you here should work. if you are having problems with it throw up some example code in your question and we can address.
spoon16
I think you want to add "descending" to the end of the order by clause here, to get the longest names first. Also "order by" should be "orderby".
Jon Skeet
+2  A: 

This answer is effectively the same as spoon16's but without using a query expression. I generally don't use a query expression for single operations (e.g. just an ordering, or just a filter, or just a projection). I figured it would be good to see the alternatives :)

var orderedList = nameList.OrderByDescending(x => x.FullName.Length);
Jon Skeet