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