• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Newbie question on Java String compareTo() function.

nace186

Platinum Member
So if I have the following string:

str1 = "158514831248";
str2 = "124875257821";

If I do str1.compareTo(str2), it should return value greater than zero right?

Basically, I have an array of string with millisecond time. I want to sort them using string compareTo(), I don't want to convert it to long for comparison.

would it work?

Thanks.
 
Let me also add that what you are trying to do is generally a bad idea. If you are trying to compare or sort numbers, use a numeric type. Strings are treated differently. For example, the string "1000000000001" would show up in a sort before the string "110" even though it is obviously a much larger number.
 
Let me also add that what you are trying to do is generally a bad idea. If you are trying to compare or sort numbers, use a numeric type. Strings are treated differently. For example, the string "1000000000001" would show up in a sort before the string "110" even though it is obviously a much larger number.

that's true. But what if the string character counts are the same?
 
that's true. But what if the string character counts are the same?

If the character counts are the same, then every high-level language I've seen will do what you're trying to accomplish. If all of your strings represent base-10 integers of equal length, then you should be alright.
 
If the character counts are the same, then every high-level language I've seen will do what you're trying to accomplish. If all of your strings represent base-10 integers of equal length, then you should be alright.

Until someone decides this must be changed or the numbers run out. i agree with your first assessment: Just do it right and use numeric types. Especially since conversion both was is trivial.
 
Not familiar with Java - but can't you just typecast on the fly?

Int(str1) > Int(str2)

EDIT:

And why are these stored in strings to begin with? Maybe that's a better location to fix the problem 🙂
 
Not familiar with Java - but can't you just typecast on the fly?

Int(str1) > Int(str2)

EDIT:

And why are these stored in strings to begin with? Maybe that's a better location to fix the problem 🙂

He could EASILY typecast on the fly. I'm guessing that they are stored as strings because they are hidden fields in an HTML form, or some other construct that uses strings for everything. That's the only good reason I can come up with for using strings for this.
 
In my short time with Java in school, our teacher taught us to use dialog boxes to enter in numbers, and those were alway strings after you entered them. We always had to cast them into whatever type we needed in order to operate on them.

Maybe this situation is similar?
 
FWIW, if you are using Amazon Simple DB services, it only can store strings, so that would be a valid reason for it to be a string. The documentation tells you to zero pad if you need to sort/compare.
 
You can't just cast a string into an int, but there are static functions on String and on Integer (valueOf) that do the conversion and throw exceptions if they couldn't get a number out of it. This is the correct way to do this, comparing Strings with numbers is a really really bad idea.
 
Back
Top