Hi,
Is there any way, out of the box, to sort a collection in alphabetical order (using C# 2.0 ?).
Thanks
Hi,
Is there any way, out of the box, to sort a collection in alphabetical order (using C# 2.0 ?).
Thanks
What sort of collections are we talking about? A List<T>
? ICollection<T>
? Array? What is the type stored in the collection?
Assuming a List<string>
, you can do this:
List<string> str = new List<string>();
// add strings to str
str.Sort(StringComparer.CurrentCulture);
List<string> stringList = new List<string>(theCollection);
stringList.Sort();
List<string>
implements ICollection<string>
, so you will still have all of the collection-centric functionality even after you convert to a list.
How about Array.Sort? Even if you don't supply a custom comparer, by default it'll sort the array in alphabetical order:
var array = new string[] { "d", "b" };
Array.Sort(array); // b, d
This may not be out of the box, but you can also use LinqBridge http://www.albahari.com/nutshell/linqbridge.aspx to do LINQ queries in 2.0 (Visual Studio 2008 is recommended though).