File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Problem about compareTo Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem about compareTo " Watch "Problem about compareTo " New topic

Problem about compareTo

david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133

There is a function to read a file and list in a table, the file has values as below:
#id, userName, emailAddress
1 = 1, name1, emailA
2 = 2, nameC, emailYY
13 = 13, name13, emailKK

when the table is ordered by id, the String.compartTo(String) method is used, and the result is like: 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9. Is this the result the compareTo supposed to have?

The result I wish is numeric order like 1,2,3,4,5,6,7,8,9,10,11,12,13.

I thought maybe the String is used for compare and get first result, so I tried Integer.compareTo(Integer) and I got error like "int cannot be dereferenced", I do not know why.

Please help to clarify my confuse.
Kiaamaa Liammes
Ranch Hand

Joined: Oct 03, 2009
Posts: 32

the compareTo method in the string class compares two string objects hence the values obtained is proper, you can override the compareTo method

John de Michele

Joined: Mar 09, 2009
Posts: 600

String's compareTo() method sort in ASCIIbetical order, not alphabetical order as might be expected.

david arnold
Ranch Hand

Joined: Apr 10, 2009
Posts: 133
I see. Thank you John and Kiaamaa for the kind help.
I agree. Here's the link:
subject: Problem about compareTo
It's not a secret anymore!