Skip to content

Instantly share code, notes, and snippets.

@YSMull
Created July 6, 2018 14:32
Show Gist options
  • Save YSMull/1226130ae4ad68cedd5457cc89a9fe21 to your computer and use it in GitHub Desktop.
Save YSMull/1226130ae4ad68cedd5457cc89a9fe21 to your computer and use it in GitHub Desktop.
import com.sun.xml.internal.fastinfoset.util.CharArray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test3 {
public static List<String> split(String s) {
List<String> res = new ArrayList<>();
int i = 0;
while (i < s.length()) {
StringBuilder numbuf = new StringBuilder();
while (Character.isDigit(s.charAt(i))) {
numbuf.append(s.charAt(i));
i++;
if (i >= s.length()) break;
}
res.add(numbuf.toString());
if (i >= s.length()) break;
StringBuilder strbuf = new StringBuilder();
while (!Character.isDigit(s.charAt(i))) {
strbuf.append(s.charAt(i));
i++;
if (i >= s.length()) break;
}
res.add(strbuf.toString());
}
System.out.println(Arrays.toString(res.toArray()));
return res;
}
public static int compareList(List<String> a, List<String> b) {
int i = 0;
while (i < a.size() && i < b.size()) {
String as = a.get(i);
String bs = b.get(i);
try {
int ia = Integer.parseInt(as);
int ib = Integer.parseInt(bs);
if (ia > ib) return 1;
if (ia < ib) return -1;
i++;
} catch (NumberFormatException e) {
if (as.compareTo(bs) > 0) return 1;
if (as.compareTo(bs) < 0) return -1;
i++;
}
}
return 0;
}
public static int compare(String a, String b) {
return compareList(split(a), split(b));
}
public static void main(String[] args) {
String a = "v1.33.m5";
String b = "v1.10.m4";
System.out.println(compare(a, b));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment