开发者

Sorting a collection in alphabetical order

Is there any way, ou开发者_开发技巧t 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);


You can use a SortedList.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜