【发布时间】:2015-06-26 10:50:11
【问题描述】:
我有一个由整数值组成的字符串列表。我只想按升序和降序排序。
ArrayList<String> a = new ArrayList<String>();
a.add("1435536000000");
a.add("1435622400000");
System.out.println("Before : " + a);
Collections.sort(a, new ComparatorOfNumericString());
System.out.println("After : " + a);
我的 ComparatorOfNumericString 类是 -
public class ComparatorOfNumericString implements Comparator<String> {
@SuppressLint("NewApi")
@Override
public int compare(String lhs, String rhs) {
int i1 = Integer.parseInt(lhs);
int i2 = Integer.parseInt(rhs);
return Integer.compare(i1, i2);
}
}
任何人知道如何在java中对具有整数值的字符串进行排序?
非常感谢!!提前!!
【问题讨论】:
-
自己做作业
-
使用集合类进行排序
-
为什么不通过在
ArrayList中转换为Integer来添加这些字符串?