I have two names from a user.
I want to compare them and display them in alphabetical order.
I tried the compareTo method, but that only returns an int.
EDIT: Answer found! Thanks again guys!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You say: “I have to names” — assuming that you meant “I have two names”…
If you have two strings, then you have only two possible ways you can return them. It’s either A, B or B, A. There are only three possible ways the two strings can be ordered.
Either:
As it turns out, according to the Java documentation, the
String.compareTo()method actually gives you a value that maps to those three possible states: