In 2.0 you can use the List<T>.Sort(Comparison<T>) overload:
List<stringlist = new List<string>();
list.Add("abc");
list.Add("abcde");
list.Add("ab");
list.Sort(delegate(string x, string y)
{ return x.Length.CompareTo(y.Length); }
);
foreach (string s in list)
{
Console.WriteLine(s);
}
The alternative would be to write a class that implements
IComparer<string- but the above is a lot easier ;-p
For reference, 3.5 (and LINQ) has a lot of support for ordering based
on projections (such as s =s.Length) - but if you have a C# 3
compiler you can still use this with .NET 2.0 via LINQBridge - then it
is as simple as:
foreach (string s in list.OrderBy(s=>s.Length))
{
Console.WriteLine(s);
}
http://www.albahari.com/nutshell/linqbridge.html
Marc