472,971 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 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 1491
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Al Newton | last post by:
I want to use STL's sort algorithm to sort a string vector. Some of the strings are fairly long (300 to 400 chars) and the vector isn't small (5,000 to 10,000 elements). Naturally, sorting time...
by: Ali Chambers | last post by:
Hi, I have a bit of a problem with a sort procedure I need to do. I have a list of items in a listbox, eg:- 2.3%<A other text here> -4%<B other text here> 10%<C other text here> -9.3%<D...
by: qazmlp | last post by:
I have got the following strings: 121721234567890 13 1217212345 129 Is it possible to do descending sort for these strings based on their numeric values? After sorting, they need to figure...
by: David | last post by:
sorry for the last post, itchy fingers. I'm having a bit of difficulty sorting images named in sequential numerical order. Here are the image names and how I need them sorted. image1.jpg...
by: Niklas Uhlin | last post by:
Why does the code below output this: "v a" (118,32,97) "w a" (119,32,97) "v b" (118,32,98) "w b" (119,32,98) "v c" (118,32,99) "w c" (119,32,99) and not this (as one would expect if...
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
by: gonzo | last post by:
So I have a project where I'm supposed to have a .txt input file of no more than ten first names, last names and birth years, and than in a menu I'm to give the user some options as to how the...
by: pabl0 | last post by:
i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted. an example is in order. ...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.