Reputation: 2412
I have some problem with comparing strings which contain integers.
Something like A11
and A9
or BA230
and BA7
or 123
and 9
I know when I want to compare integers (which are string types), I need to pass into Integer and compare
But this is not that case.
It also contains letters and numbers so I can't pass into Integer.
When I compare A11
and A9
using compareTo
method, it says A9
is bigger.
And when I compare 123
with 9
it says 9
is bigger.
Has anyone faced this issue before? Could you please help me? Thanks.
Upvotes: 0
Views: 1979
Reputation: 2412
/**
* Similar to compareTo method But compareTo doesn't return correct result for string+integer strings something like `A11` and `A9`
*/
private int newCompareTo(String comp1, String comp2) {
// If any value has 0 length it means other value is bigger
if (comp1.length() == 0) {
if (comp2.length() == 0) {
return 0;
}
return -1;
} else if (comp2.length() == 0) {
return 1;
}
// Check if first string is digit
if (TextUtils.isDigitsOnly(comp1)) {
int val1 = Integer.parseInt(comp1);
// Check if second string is digit
if (TextUtils.isDigitsOnly(comp2)) { // If both strings are digits then we only need to use Integer compare method
int val2 = Integer.parseInt(comp2);
return Integer.compare(val1, val2);
} else { // If only first string is digit we only need to use String compareTo method
return comp1.compareTo(comp2);
}
} else { // If both strings are not digits
int minVal = Math.min(comp1.length(), comp2.length()), sameCount = 0;
// Loop through two strings and check how many strings are same
for (int i = 0;i < minVal;i++) {
char leftVal = comp1.charAt(i), rightVal = comp2.charAt(i);
if (leftVal == rightVal) {
sameCount++;
} else {
break;
}
}
if (sameCount == 0) {
// If there's no same letter, then use String compareTo method
return comp1.compareTo(comp2);
} else {
// slice same string from both strings
String newStr1 = comp1.substring(sameCount), newStr2 = comp2.substring(sameCount);
if (TextUtils.isDigitsOnly(newStr1) && TextUtils.isDigitsOnly(newStr2)) { // If both sliced strings are digits then use Integer compare method
return Integer.compare(Integer.parseInt(newStr1), Integer.parseInt(newStr2));
} else { // If not, use String compareTo method
return comp1.compareTo(comp2);
}
}
}
}
Upvotes: 3
Reputation: 37
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
String.compareTo is compare every character's unicode value one by one. it will return when find that two character not equal.
A11 compare to A9:
step1: 'A' compare to 'A'
step2: '1' compare to '9' . '1' unicode value is 49, '9' unicode value is 57.
So A9 is bigger.
order_by is the same as above .
Upvotes: 0
Reputation: 239
public static String extractNumber(final String str) {
if(str == null || str.isEmpty()) return "";
StringBuilder sb = new StringBuilder();
boolean found = false;
for(char c : str.toCharArray()){
if(Character.isDigit(c)){
sb.append(c);
found = true;
} else if(found){
// If we already found a digit before and this char is not a digit, stop looping
break;
}
}
return sb.toString();
}
For input "123abc", the method above will return 123.
For "abc1000def", 1000.
For "555abc45", 555.
For "abc", will return an empty string.
// Then you can parse that to integer and then compare
Upvotes: 0