我有一个字符串数组:15MB、12MB、1TB、1GB。我想按照字典顺序进行比较,只需遵循MB小于GB和TB的规则即可。所以最后我想得到的是:12MB、15MB、1GB、1TB。我找到了一种比较字母的方法:
final static String ORDER="MGT";
public int compare(String o1, String o2) {
int pos1 = 0;
int pos2 = 0;
for (int i = 0; i < Math.min(o1.length(), o2.length()) && pos1 == pos2; i++) {
pos1 = ORDER.indexOf(o1.charAt(i));
pos2 = ORDER.indexOf(o2.charAt(i));
}
if (pos1 == pos2 && o1.length() != o2.length()) {
return o1.length() - o2.length();
}
return pos1 - pos2 ;
}
我想通过数字和字母将字符串进行分割,但是如何按字母"M B .."的顺序排序,然后按它们的数字排序。我需要使用两个比较器还是其他方法?