473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

About the Comparer.Default.Compare and culture

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
6 4407
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nick | last post by:
Why do we have the IComparable and IComparer interfaces don't they do the same thing (dispite having function which take in eitehr one and two parameters respectivly). an you give an exaple of...
3
by: MattC | last post by:
Hi, I found this code somewhere on the net and need to make some alterations. public class GenericSorter : IComparer { String sortProperty; bool sortOrder; public GenericSorter(String...
38
by: Arjang | last post by:
http://www.codeproject.com/useritems/CSharpVersusVB.asp
2
by: cindy | last post by:
i build a string string eProduct = "#blah#ablah#bbbb"; StringBuilder sb = new StringBuilder(eProduct); eProduct = sb.Replace("#","").ToString(); string aProducts= eProduct.Split(';'); then I...
2
by: alberto.ribao | last post by:
Hello, I'm making some proofs of concept about localization with .NET Framework 2.0 My trouble is I'm trying to display the next string with a call to MessageBox.Show method: ...
2
by: tommaso.gastaldi | last post by:
Hi, I need to change at runtime the Comparer of a SortedList of mine (it does not seem to expose a Comparer or Icomparer property). Do you know how can I do that (clearly without reconstructing...
3
by: QDL | last post by:
Hello everyone, I have a very simple question about arrays I have an array of Processes objects (retrieved using Process.GetProcesses()). I want to sort them descending on the WorkingSet size. ...
10
by: Tony | last post by:
Hello! I'm reading in a book and this can't be correct it says. "Objects passed to Comparer.Compare() are checked to see if they support IComparable. If they do, then that implementation is...
6
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. Now If I want to implement functionallity so I can compare animal with each other or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.