By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,502 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

About the Comparer.Default.Compare and culture

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Jun 13, 7:28 am, "Tony" <johansson.anders...@telia.comwrote:
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
Jun 27 '08 #2

P: n/a
On Thu, 12 Jun 2008 23:28:05 -0700, Tony <jo*****************@telia.com>
wrote:
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);
I'm not convinced that it is useful to do that. Do you have some specific
example where it was?

Typically, you'd use Comparer.Default when you need to pass an IComparer
implementation to something that knows less about the types being compared
than you do. I agree that if you already know you're comparing strings,
you might as well call CompareTo().
[...]
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?
A different culture, just as they say.

For example, the English language is spoken in a variety of cultures, and
there are subtly different rules based on those cultural differences, even
though the language itself is substantially the same.

Pete
Jun 27 '08 #3

P: n/a
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
Jun 27 '08 #4

P: n/a
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
Jun 27 '08 #5

P: n/a
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/libr...ltureinfo.aspx
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #6

P: n/a
On Jun 13, 7:50 am, Marc Gravell <marc.grav...@gmail.comwrote:
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>.
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
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.