What you need to use is the compareTo() method of Strings.
return this.getName().compareTo(i.getName());
That should do what you want.
Usually when implementing the Comparable interface, you will just combine the results of using other Comparable members of the class.
Below is a pretty typical implementation of a compareTo() method:
class Car implements Comparable<Car> {
int year;
String make, model;
public int compareTo(Car other) {
if (!this.make.equalsIgnoreCase(other.make))
return this.make.compareTo(other.make);
if (!this.model.equalsIgnoreCase(other.model))
return this.model.compareTo(other.model);
return this.year - other.year;
}
}
Answer from jjnguy on Stack OverflowUnderstanding the compareTo() method
comparable - How to implement compareTo method in Java and what does it mean - Stack Overflow
How to use the Comparable CompareTo on Strings in Java - Stack Overflow
Can somebody help me with java (compareto()???)
Videos
From what I have been reading, the compareTo() method returns the difference of the Unicode numerical values of two Strings when they are compared with each other. For instance, the String "hello" when compared with the String "hello" returns an integer value of zero, since they both have exactly the same Unicode characters in them. Based on my understanding of this method, "hello" should return zero when compared to "olleh", because the two Strings have the exact same Unicode characters in them. Instead, though, I am getting integer value of 7 returned to the console. Can someone break this down a bit for me to help me understand it better? Thanks in advance. Here is my code:
String str1 = "hello";String str2 = "olleh";System.out.println(str1.compareTo(str2)); // 7
What you need to use is the compareTo() method of Strings.
return this.getName().compareTo(i.getName());
That should do what you want.
Usually when implementing the Comparable interface, you will just combine the results of using other Comparable members of the class.
Below is a pretty typical implementation of a compareTo() method:
class Car implements Comparable<Car> {
int year;
String make, model;
public int compareTo(Car other) {
if (!this.make.equalsIgnoreCase(other.make))
return this.make.compareTo(other.make);
if (!this.model.equalsIgnoreCase(other.model))
return this.model.compareTo(other.model);
return this.year - other.year;
}
}
Pretty sure your code can just be written like this:
public int compareTo(Emp other)
{
return this.getName().compareTo(other.getName());
}
I have a vague idea of what it does. It returns -1,0,1 depending on the circumstances.
But this is what I don't get. If you're comparing strings, how does it behave? I'm not even sure how the code looks like.. It doesn't make sense to compare strings.
How does the ordering work for this function if you're comparing a string? Is it the length of the string?
I just don't understand it.
Also taking a look at the following website and scrolling down:
http://javarevisited.blogspot.ca/2011/11/how-to-override-compareto-method-in.html
we see a "Student" class. Specifically paying attention to :
"return (this.id < otherStudent.id ) ? -1: (this.id > otherStudent.id) ? 1:0"
What are the following strings of symbols trying to say? I don't understand any of it =/. Can somebody break it down? Where did the ? -1: come from and what does it mean?
Same with the ? 1:0
Also if we look at the same page, you'll see another class below the "Student" class.
This time, it's the same student class but implements comparable<student>. Why does the compareTo() Method return the following value? "return age - otherStudent.age;"