Home > Mobile >  How can I get a list of all strings "greater" than a specific value in a List<string>
How can I get a list of all strings "greater" than a specific value in a List<string>

Time:03-31

Consider the following scenario. I have a list of strings.

var list = new List<string> { "Ringo", "John", "Paul", "George" };

I need to sort the list and return ALL values after a specific value. For instance, if the value I need to filter off of the name "George", I want to return:

{ "John", "Paul", "Ringo" }

Sorting using standard List methods or linq is simple enough, but since these are text strings, I'm drawing a blank on figuring out how to take all values after a specific filter since you can't us a greater-than sign in your where clause.

How can I do this. Linq is preferable but not required.

CodePudding user response:

You can try quering with a help of Linq while using StringComparer:

  var list = new List<string> {"Ringo", "John", "Paul", "George" };

  string value = "George";

  var result = list
    .Where(item => StringComparer.OrdinalIgnoreCase.Compare(item, value) > 0)
    .OrderBy(item => item)
    .ToList(); // if you want to get a list as an answer
  • Related