CaseInsensitiveComparer with Generic List problem

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
 
M

Marc Gravell

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
 
J

Jon Skeet [C# MVP]

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).
 
N

Nick Olsen

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
 
N

Nick Olsen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top