471,356 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

Sort (strings)

What is the best way to sort strings by lentgh in .NET 2.o

What is the best way to implement it?
Thank u!

*** Sent via Developersdex http://www.developersdex.com ***
Jul 6 '08 #1
1 1452
In 2.0 you can use the List<T>.Sort(Comparison<T>) overload:

List<stringlist = new List<string>();

list.Sort(delegate(string x, string y)
{ return x.Length.CompareTo(y.Length); }

foreach (string s in list)

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


Jul 6 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Al Newton | last post: by
3 posts views Thread by Ali Chambers | last post: by
15 posts views Thread by David | last post: by
3 posts views Thread by Niklas Uhlin | last post: by
25 posts views Thread by Rainmaker | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
8 posts views Thread by pabl0 | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.