0
|
see: java.util.Comparator interface example (java 1.5) :List list = new ArrayList(); list.add(20); list.add(10); list.add("15"); Comparator comparator = new Comparator() { public int compare(Object o1, Object o2) { if (o1 instanceof String ) o1 = Integer.parseInt((String) o1); if (o2 instanceof String ) o2 = Integer.parseInt((String) o2); if ((Integer) o1 < (Integer) o2) return -1; if ((Integer) o1 > (Integer) o2) return 1; return 0; } }; Collections.sort(list, comparator); System.out.println( Arrays.toString(list.toArray()) );
see: java.util.Comparator interface
example (java 1.5) :List list = new ArrayList();
list.add(20);
list.add(10);
list.add("15");
Comparator comparator = new Comparator() {
public int compare(Object o1, Object o2) {
if (o1 instanceof String )
o1 = Integer.parseInt((String) o1);
if (o2 instanceof String )
o2 = Integer.parseInt((String) o2);
if ((Integer) o1 < (Integer) o2) return -1;
if ((Integer) o1 > (Integer) o2) return 1;
return 0;
}
};
Collections.sort(list, comparator);
System.out.println( Arrays.toString(list.toArray()) );
Ответил:
911911
3 года, 11 месяцев назад
| Предложить дружбу |
Добавить в эксперты
Пожаловаться на ответ
|