Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

method compareTo

Thread view: 
josh - 14 Jun 2007 08:43 GMT
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.
Andrew Thompson - 14 Jun 2007 08:48 GMT
>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?

Why not try experimentation?

Signature

Andrew Thompson
http://www.athompson.info/andrew/

josh - 14 Jun 2007 09:13 GMT
> >Hi, I've read the API description of compareTo method but I don't
> >understand
[quoted text clipped - 8 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200706/1

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??
Lars Enderin - 14 Jun 2007 09:33 GMT
josh skrev:
>>> Hi, I've read the API description of compareTo method but I don't
>>> understand
[quoted text clipped - 15 lines]
> 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.
Mike Schilling - 14 Jun 2007 09:14 GMT
> 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..
josh - 14 Jun 2007 09:28 GMT
On 14 Giu, 10:14, "Mike Schilling" <mscottschill...@hotmail.com>
wrote:
> > Hi, I've read the API description of compareTo method but I don't
> > understand
[quoted text clipped - 4 lines]
> 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 ???
Dag Sunde - 14 Jun 2007 09:51 GMT
> On 14 Giu, 10:14, "Mike Schilling" <mscottschill...@hotmail.com>
> wrote:
[quoted text clipped - 13 lines]
> 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".

Signature

Dag.

josh - 14 Jun 2007 10:25 GMT
> > On 14 Giu, 10:14, "Mike Schilling" <mscottschill...@hotmail.com>
> > wrote:
[quoted text clipped - 23 lines]
> --
> Dag.

yes I have read an ASCII table in a wrong way!
Robert Klemme - 14 Jun 2007 10:59 GMT
>>> On 14 Giu, 10:14, "Mike Schilling" <mscottschill...@hotmail.com>
>>> wrote:
[quoted text clipped - 20 lines]
>
> 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
kilian heckrodt - 14 Jun 2007 10:47 GMT
> Hi, I've read the API description of compareTo method but I don't
> understand
[quoted text clipped - 3 lines]
>
> Thanks.

lexicographical order (like dictionary or telephone book)
Matt Humphrey - 14 Jun 2007 13:09 GMT
| > Hi, I've read the API description of compareTo method but I don't
| > understand
[quoted text clipped - 5 lines]
| >
| 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 matth@ivizNOSPAM.com http://www.iviz.com/
Mike  Schilling - 14 Jun 2007 21:17 GMT
> 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....
Matt Humphrey - 15 Jun 2007 12:12 GMT
| > Dictionary order (and most text ordering) is case-insensitive, not
| > lexicographical which is strictly by comparison of each character.
[quoted text clipped - 7 lines]
|
|    ...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/index.jsp?topic=/com.sybase.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 matth@ivizNOSPAM.com http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.