CaseInsensitiveComparer with Generic List problem

  • Thread starter Thread starter Andrew Jocelyn
  • Start date Start date
A

Andrew Jocelyn

Hi

I need to do a case insensitive search on a Generic list. I'm trying to use
the BinarySearch method but I get a complier error "cannot convert from
'System.Collections.CaseInsensitiveComparer' to
'System.Collections.Generic.IComparer<string>' "

List<string> list = new List<string>();
list.Add(str1);
list.Add(str2);
list.Add(str3);
int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());

What's the correct procedure?

Thanks
Andrew
 
try: int index = list.BinarySearch(otherstr,
StringComparer.OrdinalIgnoreCase);

or InvariantCultureIgnoreCase or CurrentCultureIgnoreCase. Note that
binary search only works on sorted data; depending on what you are
doing, a SortedList<TKey,TValue> might also be worth considering,
which has an IndexOfKey method.

Marc
 
Andrew Jocelyn said:
I need to do a case insensitive search on a Generic list. I'm trying to use
the BinarySearch method but I get a complier error "cannot convert from
'System.Collections.CaseInsensitiveComparer' to
'System.Collections.Generic.IComparer<string>' "

List<string> list = new List<string>();
list.Add(str1);
list.Add(str2);
list.Add(str3);
int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());

What's the correct procedure?

Use StringComparer.*IgnoreCase (where * is Ordinal, CurrentCulture or
InvariantCulture depending on your needs).
 
Here is a blog post that shows how to do a case insensitive search on a list of strings.

http://nickstips.wordpress.com/2010/08/24/c-ignore-case-on-list-contains-method/

Hope that helps!

Nick
Hi

I need to do a case insensitive search on a Generic list. I'm trying to use
the BinarySearch method but I get a complier error "cannot convert from
'System.Collections.CaseInsensitiveComparer' to
'System.Collections.Generic.IComparer<string>' "

List<string> list = new List<string>();
list.Add(str1);
list.Add(str2);
list.Add(str3);
int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());

What's the correct procedure?

Thanks
Andrew
On Thursday, July 10, 2008 6:03 PM Jon Skeet [C# MVP] wrote:

Use StringComparer.*IgnoreCase (where * is Ordinal, CurrentCulture or
InvariantCulture depending on your needs).

--
Jon Skeet - <[email protected]>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Here is an example of how to write a case insensitive Contains method on a list of strings.

http://nickstips.wordpress.com/2010/08/24/c-ignore-case-on-list-contains-method/

Hope it helps!

Nick
Hi

I need to do a case insensitive search on a Generic list. I'm trying to use
the BinarySearch method but I get a complier error "cannot convert from
'System.Collections.CaseInsensitiveComparer' to
'System.Collections.Generic.IComparer<string>' "

List<string> list = new List<string>();
list.Add(str1);
list.Add(str2);
list.Add(str3);
int index = list.BinarySearch(otherstr, new CaseInsensitiveComparer());

What's the correct procedure?

Thanks
Andrew
On Thursday, July 10, 2008 6:03 PM Jon Skeet [C# MVP] wrote:

Use StringComparer.*IgnoreCase (where * is Ordinal, CurrentCulture or
InvariantCulture depending on your needs).

--
Jon Skeet - <[email protected]>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Back
Top