Method to sort an array of strings in descending order of number of words in each array element

Just adding a method which i wrote for an application for which the requirement was scrapped.
Hope someone can refer to this silly method. ;)

///
/// Method to sort an array of strings in descending order of number of words in each array element
///
///

Array to be sorted
/// Array sorted in descending order of number of words in each array element 
 

private static string[] SortArrayWithDescendingWordCount(string[] strArray)
{
//Array to store the number of words in each string of the array to be sorted
int[] wordLengths = new int[strArray.Length];
//variable to keep track of array index of wordLengths array.
int arrayIndex = 0;
foreach (string str in strArray)
{
//split the string in to an array of words and store the word count in wordLengths array.
wordLengths[arrayIndex] = str.Split(’ ‘).Length; ;
arrayIndex++;
}
//Sort arrays(ascending order) by taking wordLenths array as key and strArray as value
Array.Sort(wordLengths, strArray);
//now reverse strArray array to sort the array in descending order of number of words in each array element
Array.Reverse(strArray);
return strArray;
}

41 comments

I digged this for more news from you.

Regards and respect
Vixen

Nice resource. rss feed added

thanks! very helpful post!! like the template btw 😉

Do you make money out of this blog? just curious

I like what I see. keep it going

Can’t believe it it’s now 2010. Time flies! It’s that time of the year when we celebrate life, look back at past mistake and resolve to make the future better. I wish you all a very prosperous new year.

Nice resource. rss feed added

Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I’m more of a visual learner,I found that to be more helpful well let me know how it turns out!

Think of yourself as on the threshold of unparalleled success. A whole clear, glorious life lies before you. Achieve! Achieve!

With LINQ you can do this in very concise way:

using System.Linq;
public string[] SortByDecendingWordCount(string[] listOfStatements)
{
var sorted = from c in listOfStatements
orderby c.Split(‘ ‘).Length descending
select c;

return sorted.ToArray();

}

Thanks Ashish.. The above procedure was written much before Linq was released!

Accutane has harmed so many, with more coming out of the woodwork. Hopefully these people get healthy again.

this is nice post, i like this. thanks for your great information.

Do you like running Blogengine? If I might –perhaps you should consider adding a few jquery animations. I dont mean to disrespect what you’ve posted; its very enlightening, for sure. However, I think would respond to it more positively if they could be something flashy to your posts. Keep it up, but put a little more into it next time.

I truly love shoes, I’ve no idea what my problem is LOL. I merely love being able to mix match outfits which coordinate from head to toe, particularly with shoes becoming a large focal point.

You got a really useful blog I have been here reading for about half an hour. I am a newbie and your post is valuable for me.

Sex harassment lawyers California Hey – nice website, rightful superficial around few blogs, seems a pretty overnice document you are using. I’m currently using WordPress for a few of my sites but sensing to change one of them over to a structure same to yours as a experiment run. Anything in item you would urge roughly it?

I fully agree with author opinion.

Well that makes 2 things I learned this week. This and google now has a ton of greasemonkey scripts which I never knew about. You learn something every day? I got more than what I bargained for! hahaha.

There’s nothing wrong with buying yourself a gift each now and again.

Ha, actions speak louder than words. I don’t understand why more people just don’t listen.

Extremely inspiring. It’s amazing what can be done when we put our minds to it.

I’m really loving this site, do you write this by yourself or do you work with another person? What are your backgrounds?

Good points. Thanks, something to talk about with the guys at work.

//Sort arrays(ascending order) by taking wordLenths array as key and strArray as value
Array.Sort(wordLengths, strArray);

Please fix error here!

Nice information, many thanks to the author.

I hope I can remember this tomorrow to share at work – Thanks

This is a great quote. I am THE worst offender of typing up half of the page. No matter what, it always runs away from two.

You have to be warned that you will experience some withdrawal pains from nicotine. But not to worry, there are lots of nicotine patches and gums out there that will help you get through this problem. That will take care of the biological addiction, but you will still have to deal with the psychological addiction which is the biggest addiction of all.

Intimately, the post is in reality the greatest on that worthy topic. I fit in with your conclusions and will certainly thirstily look forward to your forthcoming updates. Saying thanks will certainly not simply be enough, for the awesome lucidity in your writing. I can right away grab your rss feed to stay privy of any kind of updates. Authentic work and also much success in your business endeavors!

Woot! You are at the top of google!

I totally concur with the earlier on remark, the world wide web is actually with out a question increasing in to the key type of communication worldwide and its because of to web sites such as this that concepts are dispersing so promptly.

Ugg boot is a legendary brand, you can’t understand why it has a ugly and cunbersome appearance when you have first glimpse of her.But it has been a popular boot in the Eurasian land, now popoular wind blowing all over the world today.The guide of this wind is Euramerian stars.

Thanks for posting this. i really enjoyed reading this.

Its a pity you dont have a donate button, i would donate some =)

I want to recommend anybody studying this to view the comments above opposing this blog to indicate how unnecesarry it may be.

Nice one, I love this article and I like your website.

The new Windows 7 is debugged compared to Vista. It may be a little harder to use than Window’s XP, but, it is more stable especially if you are running multiple applications.

Leave a reply