compare string integer java
Java
/**
* 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;
}
if (TextUtils.isDigitsOnly(comp1)) {
int val1 = Integer.parseInt(comp1);
if (TextUtils.isDigitsOnly(comp2)) {
int val2 = Integer.parseInt(comp2);
return Integer.compare(val1, val2);
} else {
return comp1.compareTo(comp2);
}
} else {
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) {
return comp1.compareTo(comp2);
} else {
String newStr1 = comp1.substring(sameCount), newStr2 = comp2.substring(sameCount);
if (TextUtils.isDigitsOnly(newStr1) && TextUtils.isDigitsOnly(newStr2)) {
return Integer.compare(Integer.parseInt(newStr1), Integer.parseInt(newStr2));
} else {
return comp1.compareTo(comp2);
}
}
}
}
Also in Java:
- Title
- Enums injava
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- Java.awt graphics tutorial
- Category
- Java
- Title
- java permutation
- Category
- Java
- Title
- jenkins decrypt password script console
- Category
- Java
- Title
- how to delete an element from an array in java
- Category
- Java
- Title
- elasticsearch api code call using highlevelrestclient
- Category
- Java
- Title
- java for
- Category
- Java
- Title
- creating file in specific location in java application
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- android bottom navigation hiding views
- Category
- Java
- Title
- java length of matrix
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- regex get string between quotes java
- Category
- Java
- Title
- how to print the map in java
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- selection sort in java
- Category
- Java
- Title
- caesar cipher java
- Category
- Java
- Title
- how to make a button disapear on click in javafx
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- java program to display characters from a to z using loop
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- reverse string using recursion java with explanation
- Category
- Java
- Title
- integer to string java
- Category
- Java
- Title
- how to open a text file in java
- Category
- Java
- Title
- biginteger in java
- Category
- Java
- Title
- java how to print an array
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- java output length of each line in a text document
- Category
- Java
- Title
- how to create dynamic string array in java
- Category
- Java
- Title
- how to cut a certion part from a string in java
- Category
- Java
- Title
- convert string to array java
- Category
- Java
- Title
- java round double to 2 decimal places
- Category
- Java
- Title
- java android edit text set value
- Category
- Java
- Title
- how to install java 8 on terminal os
- Category
- Java
- Title
- java calculator code
- Category
- Java
- Title
- joptionpane.showconfirmdialog yes no example
- Category
- Java
- Title
- switch en java
- Category
- Java
- Title
- java string util if empty default
- Category
- Java
- Title
- float to string java
- Category
- Java
- Title
- show menuitem when menu hovered javafx
- Category
- Java
- Title
- java random number
- Category
- Java
- Title
- java go troght loop object
- Category
- Java
- Title
- how add strings together
- Category
- Java
- Title
- spigot sounds
- Category
- Java
- Title
- jtable fill panel
- Category
- Java
- Title
- Android recycleView
- Category
- Java
- Title
- java check if int
- Category
- Java
- Title
- java interface
- Category
- Java
- Title
- JOptionPane with drop down list java
- Category
- Java
- Title
- android studio keep device awake
- Category
- Java
- Title
- java pass array as method parameter
- Category
- Java
- Title
- detect tv remote keys andoid studio
- Category
- Java
- Title
- how to add element to arraylist
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- big integer java
- Category
- Java
- Title
- java bigdecimal compareto
- Category
- Java
- Title
- java 8 hashmap example stackoverflow
- Category
- Java
- Title
- get current day java
- Category
- Java
- Title
- even or odd in java
- Category
- Java
- Title
- java is power of 2
- Category
- Java
- Title
- math minimum java
- Category
- Java
- Title
- hashmap get value by key java
- Category
- Java
- Title
- récupérer les données d un liste java
- Category
- Java
- Title
- processing sound android
- Category
- Java
- Title
- java binary exponentiation
- Category
- Java
- Title
- java string array
- Category
- Java
- Title
- check if map contains key java
- Category
- Java
- Title
- java quit application
- Category
- Java
- Title
- IN APPLet how to disable a button in java
- Category
- Java
- Title
- Don't use a line-beased input after a token-based input.
- Category
- Java
- Title
- how to close a jframe in java with an if statement
- Category
- Java
- Title
- how to create gravity in java
- Category
- Java
- Title
- imageview.setbackground
- Category
- Java
- Title
- java public static void main
- Category
- Java
- Title
- java int to binary
- Category
- Java
- Title
- array in java stack overflow
- Category
- Java
- Title
- number to string java
- Category
- Java
- Title
- web app runtime for java
- Category
- Java
- Title
- check if list contains new line in java
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- get first 5 characters of string java
- Category
- Java
- Title
- split every character in string into array java
- Category
- Java
- Title
- java initialize string array
- Category
- Java
- Title
- how to do substring java
- Category
- Java
- Title
- convert list of integer to array in java
- Category
- Java
- Title
- crit chance in java
- Category
- Java
- Title
- java rest client response json
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- java stream order by property
- Category
- Java
- Title
- boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- Category
- Java
- Title
- replace regex group() java
- Category
- Java
- Title
- how to bubblesort a string array in java
- Category
- Java
- Title
- how to initialize main in java
- Category
- Java
- Title
- java print treemap
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- summary of operator java
- Category
- Java
- Title
- multiple string java
- Category
- Java
- Title
- how to change tablayout current view position in android
- Category
- Java