About the Comparer.Default.Compare and culture

T

Tony

Hello!

My first question:
I just can't figure out what is the usefulness of
Comparer.Default.Compare(somestring1, somestring2);
because I can just the same use somestring1.CompareTo(somestring2);


One more thing is about the following text:
"Strings are processed according to the current culture. To process strings
according to a different culture(or language)
the Comparer class mus be instansiated using its constructor, which allows
you to pass a System.Globalization.CultureInfo
object specifying the culture to use."

The question is in the text they say different culture(or language) so is
culture not the same as language?
What can it then be if it's not langauge?

//Tony
 
J

Jon Skeet [C# MVP]

My first question:
I just can't figure out what is the usefulness of
Comparer.Default.Compare(somestring1, somestring2);
because I can just the same use somestring1.CompareTo(somestring2);

But you can pass Comparer.Default to things which need to use an
IComparer, such as Dictionary. So you can tell the dictionary how to
compare particular things.
One more thing is about the following text:
"Strings are processed according to the current culture. To process strings
according to a different culture(or language)
the Comparer class mus be instansiated using its constructor, which allows
you to pass a System.Globalization.CultureInfo
object specifying the culture to use."

The question is in the text they say different culture(or language) so is
culture not the same as language?
What can it then be if it's not langauge?

Culture is not the same as language, although the distinctions can be
subtle. Consider the UK English and US English cultures. They both use
the language "English" (although obviously even the language has
variances in terms of spelling etc) but they format dates differently.

Jon
 
M

Mihai N.

The question is in the text they say different culture(or language) so is
culture not the same as language?
What can it then be if it's not langauge?

Culture is more than language.
It is what everybody else calls "locale" (why .NET had to be different,
I don't know)

Thing about en_US vs en_GB.
The laguage is en (English). But it is not enough for determine the
number/date/time formats, for instance. Even the UI might be different.
Same for a lot of other languages that are used in more than a region.

In fact, one cannot even decide for basic stuff like date formats,
or measurement system based on language only.
(for English language should I use the metric system? :)


There is also the script that affects the culture
(ie Serbian Cyrillic vs Serbian Latin)

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
 
M

Marc Gravell

A "culture" is a larger concept for language - it encompasses things
like numeric formats (period/comma for decimal portion, currency
symbol), date-time formats (dd/MMM/yyyy etc), calendar, and language
(collation). In terms of a string-compare, I agree it would seem like
the langauge (collation) is the most significant.

Yes, Comparer.Default.Compare(string2, string2) is a long-winded way
to do it. Of course, with somestring1.CompareTo(somestring2) you first
need to know that somestring1 isn't null, where-as the comparer will
worry about this internally.

But if you are writing a sort algorithm, you just want a comparer -
and Comparer.Default (or Comparer<T>.Default) is a good choice if the
caller hasn't supplied one. When treated in the more generic sense,
when you aren't sure what the type is, and you don't know (in terms of
convincing the compiler) that the type implements IComparable and/or
IComparable<T>, it is a very versatile helper.

Marc
 
M

Marc Gravell

But you can pass Comparer.Default to things which need to use an
IComparer, such as Dictionary. So you can tell the dictionary how to
compare particular things.

SortedDictionary<,> / SortedList<,> surely; Dictionary<,> wants an
IEqualityComparer<T>.

Marc
 
J

Jon Skeet [C# MVP]

SortedDictionary<,> / SortedList<,> surely; Dictionary<,> wants an
IEqualityComparer<T>.

Doh. That's what I get for posting from a machine which doesn't have
MSDN on to double check these things when I haven't had enough coffee
to be fully awake.

(You might argue that I just shouldn't post when fully awake...)

Jon
 

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