Sorting the collection in alphabetical order

Is there any way from sorting to sort a collection in alphabetical order (using C # 2.0?).

thanks

+4
source share
5 answers

What collections are we talking about? A List<T> ? ICollection<T> ? An 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); 
+11
source

You can use a SortedList .

+4
source

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 
+3
source
 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.

+1
source

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).

+1
source

All Articles