method compareTo

J

josh

Hi, I've read the API description of compareTo method but I don't
understand
how is calculated the returning value when strings aren't equals!

Any more simple explanation?

Thanks.
 
J

josh

Why not try experimentation?

Yes I made it but in some examples I don't understand the result.

String a = "I am a string!";
String b = "i am a string!";
int rp = a.compareTo(b); // -32

here rp is -32 and for understanding that value I must check the
correspondent Unicode different values??
 
M

Mike Schilling

josh said:
Hi, I've read the API description of compareTo method but I don't
understand
how is calculated the returning value when strings aren't equals!

Any more simple explanation?

Think about the simplest possible algorithm for determining whether two
strings are equal, and the difference function it would lead to..
 
J

josh

Think about the simplest possible algorithm for determining whether two
strings are equal, and the difference function it would lead to..

I learned it use lexicographic algorithm ... it mean that if I have:
String s = "a";
String t = "g";

than s.compareTo(t) return -6 because g is far 6 letters from a ....
but why -32 in my examples ???
 
L

Lars Enderin

josh skrev:
Yes I made it but in some examples I don't understand the result.

String a = "I am a string!";
String b = "i am a string!";
int rp = a.compareTo(b); // -32

here rp is -32 and for understanding that value I must check the
correspondent Unicode different values??
Unicode, ASCII, iso-8859-1, whatever...
The value of 'I' is 32 less than 'i' in any case. The difference is
negative, which is the information you need for sorting. Thus the first
character values in the two strings that differ determine the relation.
 
D

Dag Sunde

josh said:
I learned it use lexicographic algorithm ... it mean that if I have:
String s = "a";
String t = "g";

than s.compareTo(t) return -6 because g is far 6 letters from a ....
but why -32 in my examples ???

As someone said... ASCII, ISO-xxx, whatever.
All assigns a numerical position to the characters in the charset.

Take a look at an ASCII table (http://www.asciitable.com/) and count,
and you will see that just like "g" it 6 letters higher that "a",
You will see that "i" is 32 letters higher than "I".
 
J

josh

As someone said... ASCII, ISO-xxx, whatever.
All assigns a numerical position to the characters in the charset.

Take a look at an ASCII table (http://www.asciitable.com/) and count,
and you will see that just like "g" it 6 letters higher that "a",
You will see that "i" is 32 letters higher than "I".

yes I have read an ASCII table in a wrong way!
 
K

kilian heckrodt

josh said:
Hi, I've read the API description of compareTo method but I don't
understand
how is calculated the returning value when strings aren't equals!

Any more simple explanation?

Thanks.
lexicographical order (like dictionary or telephone book)
 
R

Robert Klemme

yes I have read an ASCII table in a wrong way!

Actually ASCII values do not matte at all here - the exact return value
is just an implementation detail. The important bit is the information
that the return value is less than, equal to or greater than zero which
is mandated by the contract of Comparable.

If you want to know how it's implemented in the case of String you can
look into the sources of the std lib. It's all there.

Kind regards

robert
 
M

Matt Humphrey

| josh wrote:
| > Hi, I've read the API description of compareTo method but I don't
| > understand
| > how is calculated the returning value when strings aren't equals!
| >
| > Any more simple explanation?
| >
| > Thanks.
| >
| lexicographical order (like dictionary or telephone book)

Dictionary order (and most text ordering) is case-insensitive, not
lexicographical which is strictly by comparison of each character.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
M

Mike Schilling

Matt Humphrey said:
Dictionary order (and most text ordering) is case-insensitive, not
lexicographical which is strictly by comparison of each character.

Dictionary doesn;t ignore case; what it does is make case the least
significant difference. That is, instead of the Unicode ordering

...ABC...XYZ...abc./.xyz...

dictionary order is

...AaBbCc...XxYyZz....
 
M

Matt Humphrey

|
| | > Dictionary order (and most text ordering) is case-insensitive, not
| > lexicographical which is strictly by comparison of each character.
|
| Dictionary doesn;t ignore case; what it does is make case the least
| significant difference. That is, instead of the Unicode ordering
|
| ...ABC...XYZ...abc./.xyz...
|
| dictionary order is
|
| ...AaBbCc...XxYyZz....

I see what you mean. Google turned up many different ways for ordering
dictionaries and phone books. Here's what Sybase means by "dictionary
order".
http://infocenter.sybase.com/help/i...ase.dc38421_1500/html/ntconfig/ntconfig86.htm
and there are also special orderings for phone books. "Dictionary order"
depends on whose dictionary you're talking about and is rarely ever the
natural lexicographical order of the characters.

Matt Humphrey (e-mail address removed) http://www.iviz.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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top