Below Algo "compare two strings lexicographically"
Input two strings string 1 and string 2.
for (int i = 0; i < str1.length() && i < str2.length(); i ++)
(Loop through each character of both strings comparing them until one of the string terminates):
a. If unicode value of both the characters is same then continue;
b. If unicode value of character of string 1 and unicode value of string 2 is different then return (str1[i]-str2[i])
if length of string 1 is less than string2
return str2[str1.length()]
else
return str1[str2.length()]
// This method compares two strings lexicographically
public static int compareCustom(String s1, String s2) {
for (int i = 0; i < s1.length() && i< s2.length(); i++) {
if(s1.charAt(i) == s2.charAt(i)){
//System.out.println("Equal");
continue;
}
else{
return s1.charAt(i) - s2.charAt(i);
}
}
if(s1.length()<s2.length()){
return s2.length() - s1.length();
}
else if(s1.length()>s2.length()){
return s1.length()-s2.length();
}
else{
return 0;
}
}
if two String are equal it will return 0 otherwise return Negative or positive value
Source : - Source