I am learning about arrays, and basically I have an array that collects a last name, first name, and score.
I need to write a compareTo
method that will compare the last name and then the first name so the list could be sorted alphabetically starting with the last names, and then if two people have the same last name then it will sort the first name.
I'm confused, because all of the information in my book is comparing numbers, not objects and Strings.
Here is what I have coded so far. I know it's wrong but it at least explains what I think I'm doing:
public int compare(Object obj) // creating a method to compare
{
Student s = (Student) obj; // creating a student object
// I guess here I'm telling it to compare the last names?
int studentCompare = this.lastName.compareTo(s.getLastName());
if (studentCompare != 0)
return studentCompare;
else
{
if (this.getLastName() < s.getLastName())
return - 1;
if (this.getLastName() > s.getLastName())
return 1;
}
return 0;
}
I know the <
and >
symbols are wrong, but like I said my book only shows you how to use the compareTo
.
I wouldn't have an Object type parameter, no point in casting it to Student if we know it will always be type Student.
As for an explanation, "result == 0" will only occur when the last names are identical, at which point we compare the first names and return that value instead.
You're almost all the way there.
Your first few lines, comparing the last name, are right on track. The compareTo() method on string will return a negative number for a string in alphabetical order before, and a positive number for one in alphabetical order after.
Now, you just need to do the same thing for your first name and score.
In other words, if Last Name 1 == Last Name 2, go on a check your first name next. If the first name is the same, check your score next. (Think about nesting your if/then blocks.)
Consider using the
Comparator
interface described here which uses generics so you can avoid castingObject
toStudent
.As Eugene Retunsky said, your first part is the correct way to compare
String
s. Also if thelastName
s are equal I think you meant to comparefirstName
s, in which case just usecompareTo
in the same way.Listen to @milkplusvellocet, I'd recommend you to implement the Comparable interface to your class as well.
Just contributing to the answers of others:
String.compareTo()
will tell you how different a string is from another.e.g.
System.out.println( "Test".compareTo("Tesu") );
will print -1 andSystem.out.println( "Test".compareTo("Tesa") );
will print 19and nerdy and geeky one-line solution to this task would be:
return this.lastName.equals(s.getLastName()) ? this.lastName.compareTo(s.getLastName()) : this.firstName.compareTo(s.getFirstName());
Explanation:
this.lastName.equals(s.getLastName())
checks whether lastnames are the same or notthis.lastName.compareTo(s.getLastName())
if yes, then returns comparison of last name.this.firstName.compareTo(s.getFirstName())
if not, returns the comparison of first name.The compareTo method is described as follows:
Let's say we would like to compare Jedis by their age:
Then if our Jedi is older than the provided one, you must return a positive, if they are the same age, you return 0, and if our Jedi is younger you return a negative.
By implementing the
compareTo
method (coming from theComparable
interface) your are defining what is called a natural order. All sorting methods in JDK will use this ordering by default.There are ocassions in which you may want to base your comparision in other objects, and not on a primitive type. For instance, copare Jedis based on their names. In this case, if the objects being compared already implement
Comparable
then you can do the comparison using itscompareTo
method.It would be simpler in this case.
Now, if you inted to use both name and age as the comparison criteria then you have to decide your oder of comparison, what has precedence. For instance, if two Jedis are named the same, then you can use their age to decide which goes first and which goes second.
If you had an array of Jedis
All you have to do is to ask to the class
java.util.Arrays
to use its sort method.This
Arrays.sort
method will use yourcompareTo
method to sort the objects one by one.A String is an object in Java.
you could compare like so,