string integer compare java

Solutions on MaxInterview for string integer compare java by the best coders in the world

showing results for - "string integer compare java"
Henley
01 Jan 2019
1/**
2    * Similar to compareTo method But compareTo doesn't return correct result for string+integer strings something like `A11` and `A9`
3*/
4
5private int newCompareTo(String comp1, String comp2) {
6  // If any value has 0 length it means other value is bigger
7  if (comp1.length() == 0) {
8    if (comp2.length() == 0) {
9      return 0;
10    }
11    return -1;
12  } else if (comp2.length() == 0) {
13    return 1;
14  }
15  if (TextUtils.isDigitsOnly(comp1)) {
16    int val1 = Integer.parseInt(comp1);
17    if (TextUtils.isDigitsOnly(comp2)) {
18      int val2 = Integer.parseInt(comp2);
19      return Integer.compare(val1, val2);
20    } else {
21      return comp1.compareTo(comp2);
22    }
23
24  } else {
25
26    int minVal = Math.min(comp1.length(), comp2.length()), sameCount = 0;
27
28    // Loop through two strings and check how many strings are same
29    for (int i = 0;i < minVal;i++) {
30      char leftVal = comp1.charAt(i), rightVal = comp2.charAt(i);
31      if (leftVal == rightVal) {
32        sameCount++;
33      } else {
34        break;
35      }
36    }
37    if (sameCount == 0) {
38      return comp1.compareTo(comp2);
39    } else {
40      String newStr1 = comp1.substring(sameCount), newStr2 = comp2.substring(sameCount);
41      if (TextUtils.isDigitsOnly(newStr1) && TextUtils.isDigitsOnly(newStr2)) {
42        return Integer.compare(Integer.parseInt(newStr1), Integer.parseInt(newStr2));
43      } else {
44        return comp1.compareTo(comp2);
45      }
46    }
47  }
48}
Nicolò
17 Jan 2016
1num == Integer.parseInt(str) is going to faster than str.equals("" + num)
2
3str.equals("" + num) will first convert num to string which is O(n) where n being the number of digits in the number. Then it will do a string concatenation again O(n) and then finally do the string comparison. String comparison in this case will be another O(n) - n being the number of digits in the number. So in all ~3*O(n)
4
5num == Integer.parseInt(str) will convert the string to integer which is O(n) again where n being the number of digits in the number. And then integer comparison is O(1). So just ~1*O(n)
6
7To summarize both are O(n) - but str.equals("" + num) has a higher constant and so is slower.
queries leading to this page
compare to integeres javahow to compare string numbers in javacompare numerical string javahow to compare int in javajava comparing intscompare length of int javaintegerformat javacompare string with intinteger compare function javajava integer comparecan you compare int to string in javainteger compare ni javacomparing integer to stringnumber string comparison javahow to compare two integersin javastring integer compare javacompareto java with intcompare string integer javacompare integer int javainteger comparison javacompare integers in javacompare two ints javainteger comparision and string comparisionhow to compare number string and integer in javacompareto two integer in javahow to use compareto for int in javacompare two integer in javacomparing numbers javacompare strings to integers javacompare numbers in javainteger compare values javahow to compare ints javajava integer compareint comparison in javajava comparing two numberscompare integer with int javahow to compare numbers with strings javajava int with specific didgitscan i compare int and string in javacomparing integers in javahow to compare string to int in javahow to compare two integers in javahow to compare ints in javacompare 2 ints javainteger compare javajava compate strings integerscompare to integer in javacompare integer javahow to compareto ints in javajava compare strings as numbersstring 5b0 5d to compare to a intcomparison between integers and string in javacompareto integer javacompareto method java integerscomparing ints in javajava compare intsint compare javajava compare int valueshow to compare string and integer in javahow to compare numbers javahow to compare numbers in javajava compare two integercompare string and int javajava compare class for integerinteger compare in javahow to compare string element and an integercompare int using javacompareto method integer javajava comparing numbershow java compareto string digitcompare int 5b 5d javahow to compare integer values in javacomparingint javacompare to integer javajava compare two number stringscompare integer java with 3d 3djava string compare value as string 7c 7c integerjava compare string to intcompare integer in javajava compare primitive intcompare int and integer javajava compare strings integershow to compare two int values in javainteger comparison in javacompare integer strings javajava compareto to compare intsjava comparing int values in string syntaxinteger compare in javahow to compare strings in java that have numberscompare 3 int javaint comparison javainteger compare javacompare string intscompare if int in javajava code for comparing two intsinteger compare javainteger compare javacompare ints javajava compare two intsjava int comparetocompareto java intcompare int and string javacan you compare string integers in javacompareto java for intcompare two integers javahow do i compare integers in javajava compare integersjava compare intcomparer integer strings javacompare string to int in javacompare number string in javajava compare integerjava compare with two intcomparing numbers inside a string javajava integer value comparisonhow to compare two integer string in javacompare number in javacompareto method java integerhow to compare a string and an integer in javacompare ints jacomparison in java with intcompare primitive int javajava compareto integerjava compareto intsstring and integer comaprisonjava compareto fot inthow to compare int with string in javahow to compare string with integerjava integer compare using 3d 3djava int compare tocompare integers javajava compare two inthow to compare long with intcan we compare a string with a string and integercompare two integer javajava compare numbers java string compare any numbercompareto string with integers javahow to compare string and int in javahow to compare integers in javahow to compare integer value in javahow to compare to ints in javacompare two java integerhow to compare integer and string in javahow do i compare all integers in java java compare int numbers how to compare integer in javacompare int in javacompare int and integer in javajava 26 comparing intcompareto int javajava how to compare 2 integerscompare two int numbers javainteger string comparisoncompareto method in java for integershow to compare two integer javajava comparing integerscompare int javacompareto with strings that contain integers javajava compare string with intcompare to function integers javahow to compare int value of string in javahow to compare 4 integers in javacompare integer string ifcompare to int javajava compare string of intsjava compareto inthow to compare number in javajava compare two integersstring integer compare greater javajava compare two integerrjava int comparetojava comparing int and integercomparing string numbers javacompare equal int javajava compare 2 integersjava compare to intjava compare int with integerjava compare 2 intjava compare integer functionjava compare int with intint compare to javahow to compare two int in javajava compare integers equalitycompare 2 integers in javacompare integer and int javajava compare bidimensional int with stringjava compareintcomparing and int to a variable javahow to compare two ints javajava int comparisonhow to compare two numeric strings in javawhen int convert to string condition in javacompare string of numbers javacomparing integers javacompare two int javacompare two integers in javacomparing int and integer javahow to write compareto for integer javacompare numeric string javaint compare javajava comparator with string and intcompareto with integers java compareto with integers javacompare string and int value in javastring to integer compare in javastring compare with int in javaint compareto javacompare to java intcan i compare int and integer javacan you can compare a string to an integer in javastring integer compare java