Click here to Skip to main content
15,891,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to sort the data stored in any one of following Array, List, DataTable ...

For example, if I sort the following :

1) house money money
2) money
3) money house
4) house
5) Is money important to us

The result should be :

4) house
2) money
3) money house
1) house money money
5) Is money important to us

Awaiting your response.

Thanks
Kapil
Posted
Comments
[no name] 29-Feb-12 13:44pm    
What is the criteria behind this sort? It neither seems to be numerical nor alphabetical.
Kapil Waghe 29-Feb-12 13:50pm    
It is not alphanumerical. I want to sort it according the number of keyword in a phrase i.e. "house money money" has 3 keywords, "Is money important to us" has 5 keywords and so on.

Using linq, it's a simple one liner:

C#
var results = vals.OrderBy(x => x.Split(' ').Count());


Given that vals is your string array, i.e.

C
var vals = new string[]
{
    "house money money",
    "money",
    "money house",
    "house",
    "Is money important to us",
};
 
Share this answer
 
You would need to implement a custom IComparer[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900