int (String n1, String n2) { // trim leading zeros while(n1.length() > 0 && n2.length() > 0 && n1.charAt(0) == '0' && n2.charAt(0) == '0') { n1 = n1.substring(1); n2 = n2.substring(1); } // starts with zero => less if (n1.length() > 0 && n1.charAt(0) == '0') { return -1; } else if (n2.length() > 0 && n2.charAt(0) == '0') { return 1; } // compare as numbers final int n1len = n1.length(); final int n2len = n2.length(); if (n1len > n2len) { return 1; } else if (n2len > n1len) { return -1; } return n1.compareTo(n2); }