Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{ "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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading