1. Member
Join Date
Dec 2008
Posts
55
Rep Power
0

## 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. 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. Member
Join Date
Dec 2008
Posts
55
Rep Power
0
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.

4. Member
Join Date
Dec 2008
Posts
14
Rep Power
0

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

And look there for the explanation of the compareTo method..

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

7. Senior Member
Join Date
Nov 2008
Posts
286
Rep Power
12
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. Member
Join Date
Dec 2008
Posts
55
Rep Power
0
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...

#### Posting Permissions

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