tags:

views:

119

answers:

5

How to implement case insensitive comparison?

List<Person> persons = new List<Person>();
persons.Add(new Person("P005", "Janson"));
persons.Add(new Person("P002", "Arnold"));
persons.Add(new Person("P007", "Kazhal"));
persons.Sort((p1, p2) => p1.Name.CompareTo(p2.Name));
+14  A: 
persons.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true));
Diego Jancic
A: 

Use new CaseInsensitiveComparer().CompareTo(str1, str2)

klausbyskov
+5  A: 

Do you mean for Strings?

String.Compare(string1, string2, true);

or, the dirty way...

string1.ToLower() == string2.ToLower()
Bobby
+1  A: 

You could just call p1.Name.ToLower().CompareTo(p2.Name.ToLower()) for a quick implementation. Probably not the best long term solution though.

Jimmeh
+2  A: 

"abc".Equals("ABC", StringComparison.OrdinalIgnoreCase)

Petoj