【发布时间】:2013-08-16 06:30:28
【问题描述】:
我有从自定义对象获取的字符串格式的价格值,如下所示
2,000,3,000,5,000,300,-,600,-,507,-
我想按升序和降序对上述价格值进行排序
有人可以帮忙吗?
我试过这个:
Comparator<Cars> comparator = new Comparator<Cars>() {
@Override
public int compare(Cars object1, Cars object2) {
// return Float.compare(Integer.parseInt(object1.getPrice()), Integer.parseInt(object2.getPrice()));
String price1=object1.getPrice().replaceAll(",","");
price1=price1.replaceAll("-","");
String price2=object2.getPrice().replaceAll(",","");
price2=price2.replaceAll("-","");
return ((Integer)Integer.parseInt(price1)).compareTo((Integer)Integer.parseInt(price2));
}
};
但是如果我执行语句
Collections.sort(carsList, 比较器);
获取异常
例外:Error:java.lang.NumberFormatException: Invalid int: ""
【问题讨论】: