i have a class:
public class Essay
{
public int ID{get;set;}
public string Name{get;set;}
}
and list of Essay type
List<Essay> essays=new List<Essay>();
on the name property contains numbers and letters.
i want to sort the list by the name property
for example:
essays=
{1,"ccccc"},
{2,"aaaa"},
{3,"bbbb"},
{4,"10"},
{5,"1"},
{6,"2"},
{7,"1a"}
i want to sort:
essays=
{2,"aaaa"},
{3,"bbbb"},
{1,"ccccc"},
{5,"1"},
{7,"1a"},
{6,"2"},
{4,"10"}
how i do it?
thank to all.
There are several elements to the answer.
The first part is being able to in-place sort a List using Sort() and a lambda comparison method. That’s solved by using an extension method for IList and a helper “ComparisonDelegator” class. Combining those, it’s possible to pass a lambda to List.Sort().
The second part has been addressed in another post here (which I have upvoted) and the code from which I have shamelessly pasted into the AlphanumComparator class in this answer.
(As a side note, I should point out that all the Linq examples posted elsewhere in this thread make a COPY of the list. This is fine for short lists, but if you have a long list it can cause performance problems. The solution presented here does NOT make a copy of the list.)
Putting it all together, we get the following code, which outputs:
And the full code sample (compilable as a console application):