MartinZPetrov
MartinZPetrov

Reputation: 316

Sort or Order a List of strings containing a specific word with LINQ

I have the following sentances.

but the TEXT is more than TEXT to TEXT

and you need a better TEXT to TEXT

cause this TEXT is too much about TEXT and a TEXT will never teach you how to TEXT

if you have someone to TEXT

try to TEXT what the TEXT would want to TEXT another TEXT with TEXT

So is it possible to order or sort the List with LINQ so I have have a result with the most encountered "Text" from top to bottom.

It should look like:

try to TEXT what the TEXT would want to TEXT another TEXT with TEXT

cause this TEXT is too much about TEXT and a TEXT will never teach you how to TEXT

but the TEXT is more than TEXT to TEXT

and you need a better TEXT to TEXT

if you have someone to TEXT

Upvotes: 0

Views: 987

Answers (1)

EZI
EZI

Reputation: 15354

var sortedLines = list.OrderByDescending(line => Regex.Matches(line, "TEXT").Count)
                      .ToList();

PS: A better Regex would be Regex.Matches(line, @"[\W^]*TEXT[\W$]*")

Upvotes: 5

Related Questions