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
How to use the Comparable CompareTo on Strings in Java - Stack Overflow
java - String compareTo understanding - Stack Overflow
java - What does the String classes compareTo() method return - Stack Overflow
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());
}
You seem to have misunderstood what is meant by "lexicographic order".
Since c comes after b, s1 is considered bigger than s2!
Think of letters as numbers. a is 1, b is 2, c is 3, and so on. 2 comes after 1, so 2 is bigger than 1.
So comparing bb and bc is just like comparing 22 and 23. Obviously, 23 is bigger.
Assuming below is the below program as per your code snippet:
Copypackage devsought;
public class JavaStringCompareTo {
public static void main(String... args) {
String s1="bc";
String s2="bb";
System.out.println(s1.compareTo(s2));
}
}
The output is 1
Explanation:
| b (index 0) | c (index 1) |
|---|---|
| b (index 0) | b (index 1) |
At index 1(second position),we have two characters โcโ(unicode value \u0063,Decimal value 99) and โbโ(unicode value \u0062 ,Decimal value 98).The difference 99-98=1 is returned and printed on the console. Check out for more examples on java String compareTo method
As far as you're concerned, there's no telling what the magnitude of the compareTo return value is, just the sign. In practice, most compareTo implementations will return -1, 0, or 1, but the contract specifically says positive or negative, and you should write your code accordingly (e.g., using int compare = a.compareTo(b); if(compare > 0) {...} else...).
According to http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo%28java.lang.String%29
In this case, compareTo returns the difference of the two character values at position k >in the two string -- that is, the value:
this.charAt(k)-anotherString.charAt(k)If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
this.length()-anotherString.length()
For the last case, for the lengths of the String, by documentation that seems it can return other than -1, 0, 1