method compareTo

Discussion in 'Java' started by josh, Jun 14, 2007.

  1. josh

    josh Guest

    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.
     
    josh, Jun 14, 2007
    #1
    1. Advertising

  2. Andrew Thompson, Jun 14, 2007
    #2
    1. Advertising

  3. josh

    josh Guest

    On 14 Giu, 09:48, "Andrew Thompson" <u32984@uwe> wrote:
    > 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?

    >
    > Why not try experimentation?
    >
    > --
    > Andrew Thompsonhttp://www.athompson.info/andrew/
    >
    > 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??
     
    josh, Jun 14, 2007
    #3
  4. 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?


    Think about the simplest possible algorithm for determining whether two
    strings are equal, and the difference function it would lead to..
     
    Mike Schilling, Jun 14, 2007
    #4
  5. josh

    josh Guest

    On 14 Giu, 10:14, "Mike Schilling" <>
    wrote:
    > 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?

    >
    > 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 ???
     
    josh, Jun 14, 2007
    #5
  6. josh

    Lars Enderin Guest

    josh skrev:
    > On 14 Giu, 09:48, "Andrew Thompson" <u32984@uwe> wrote:
    >> 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?

    >> Why not try experimentation?
    >>
    >> --
    >> Andrew Thompsonhttp://www.athompson.info/andrew/
    >>
    >> 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??
    >
    >

    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.
     
    Lars Enderin, Jun 14, 2007
    #6
  7. josh

    Dag Sunde Guest

    josh wrote:
    > On 14 Giu, 10:14, "Mike Schilling" <>
    > wrote:
    >> 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?

    >>
    >> 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 ???


    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".

    --
    Dag.
     
    Dag Sunde, Jun 14, 2007
    #7
  8. josh

    josh Guest

    On 14 Giu, 10:51, "Dag Sunde" <> wrote:
    > josh wrote:
    > > On 14 Giu, 10:14, "Mike Schilling" <>
    > > wrote:
    > >> 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?

    >
    > >> 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 ???

    >
    > 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".
    >
    > --
    > Dag.


    yes I have read an ASCII table in a wrong way!
     
    josh, Jun 14, 2007
    #8
  9. 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)
     
    kilian heckrodt, Jun 14, 2007
    #9
  10. On 14.06.2007 11:25, josh wrote:
    > On 14 Giu, 10:51, "Dag Sunde" <> wrote:
    >> josh wrote:
    >>> On 14 Giu, 10:14, "Mike Schilling" <>
    >>> wrote:
    >>>> 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?
    >>>> 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 ???

    >> 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".
    >>
    >> --
    >> Dag.

    >
    > 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
     
    Robert Klemme, Jun 14, 2007
    #10
  11. "kilian heckrodt" <> wrote in message
    news:f4r383$6ar$00$-online.com...
    | 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 http://www.iviz.com/
     
    Matt Humphrey, Jun 14, 2007
    #11
  12. "Matt Humphrey" <> wrote in message
    news:...
    > 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....
     
    Mike Schilling, Jun 14, 2007
    #12
  13. "Mike Schilling" <> wrote in message
    news:Gnhci.1198$...
    |
    | "Matt Humphrey" <> wrote in message
    | news:...
    | > 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 http://www.iviz.com/
     
    Matt Humphrey, Jun 15, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. natG
    Replies:
    5
    Views:
    397
    Chris Smith
    Dec 17, 2003
  2. Nishi Bhonsle

    compareTo() method

    Nishi Bhonsle, Jan 20, 2004, in forum: Java
    Replies:
    2
    Views:
    22,029
    Nishi Bhonsle
    Jan 21, 2004
  3. Paul Morrison

    Implementing the compareTo method

    Paul Morrison, Jan 9, 2006, in forum: Java
    Replies:
    20
    Views:
    7,561
    Paul Morrison
    Jan 10, 2006
  4. chucky
    Replies:
    29
    Views:
    780
    Piotr Kobzda
    Sep 28, 2007
  5. rocafella5007

    Need help with compareTo method

    rocafella5007, Mar 25, 2009, in forum: Java
    Replies:
    0
    Views:
    572
    rocafella5007
    Mar 25, 2009
Loading...

Share This Page