Results 1 to 8 of 8
  1. #1
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default comparing strings

    I'm writing a program that uses the merge sort algorithm. The program is supposed to sort an array list of strings into alphabetical order. Right now I'm using the compareTo method... does this check if it is alphabetized? If not, how would I do so? Thanks..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Not sure what you're asking here. The compareTo does only what you tell it to do. If you will be sorting using your own algorithm, then you'll likely use the compareTo method to help make decisions about how to sort a piece of data.

  3. #3
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Well I assigned the values I'm comparing to two strings, named s1 and s2 respectively. I made an if statement saying:

    if(s1.compareTo(s2) <= 0) {
    ....
    }

    What I'm trying to find out is whether the compareTo method for strings will return 1 if s1 is alphabetically before s2. If not, what does it do?

    My program right now isn't sorting it properly so I think the error may lie there but I'm not sure.

    Hope that made things clearer.

  4. #4
    PatoDB is offline Member
    Join Date
    Dec 2008
    Posts
    14
    Rep Power
    0

    Default

    Maybe this web can help you...

    There is an explanation of the method compareTo()

    Look into the API web page for the method compareTo

    Currently i'm not able to post links, but..

    Google -> String API

    And look there for the explanation of the compareTo method..
    ..:: abretumundo.wordpress.com ::..

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    When you say alphabetically, do you mean you want to ignore case? "A".compareTo("a") does not return 0, but "A".compareToIgnoreCase("a") does.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    From what I understand from the compareTo method definition, it should work, although I would recommend what Steve states, if the characters case is not an issue.

    compareTo method:
    This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

    this.charAt(k)-anotherString.charAt(k)

    If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

    this.length()-anotherString.length()


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    The String compareTo() method is essentially based on the "raw" Unicode value of the characters (BTW, to the OP, this shouldn't be a mystery -- you're allowed to look at the JDK source code, you know!).

    If you want something a bit more "strictly alphabetical" for a given language, then look at the Collator class.

  8. #8
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Thanks guys. I finally found out what I did wrong. It didn't even have to do with the compareTo.... I just didn't know that the String.toLowerCase() didn't actually change the string. It's always a dumb error like this...

Similar Threads

  1. Replies: 2
    Last Post: 12-06-2008, 06:54 PM
  2. Problem Comparing Strings (its not what you think)
    By hilather in forum New To Java
    Replies: 7
    Last Post: 11-19-2008, 06:43 PM
  3. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  4. JSTL -- Comparing two strings for equality
    By trinkets in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-12-2008, 04:39 PM
  5. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •