• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem about compareTo

 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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
Posts: 32
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony,

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

John.
 
david arnold
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Thank you John and Kiaamaa for the kind help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic