Nishanth's Blog | Method to sort an array of strings in descending order of number of words in each array element

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

by Nishanth 2. November 2008 06:45

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;
}

Tags: , , ,

Comments

4/28/2010 6:38:10 PM #

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


        }

Ashish India

4/28/2010 7:08:33 PM #

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

nishanth India

7/29/2010 9:46:20 AM #

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

Please fix error here!

Pharmacie en ligne United States

8/11/2010 1:50:15 PM #

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)

Moslioen United States

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen


About Me

Nishanth Nair  

 A die hard fan of Rise of Nations... I write few softwares for McAfee Inc. for a living.

 

Portfolio:

http://morningbellnews.com
http://Necab.org
http://SufiKatha.com
http://DeepaRecipes.com
http://ManipalFoundation.in
http://NetraArts.com
http‌://EnWebTech.com
Contact : Nair.Nishanth@Gmail.com 

 

All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer.

Tag cloud

free counters