Oi, tenho uma tabela desse tipo ArrayList<ArrayList> table, que cada indice de table é uma row e cada row tem n elementos e
para passar essa tabela para um csv eu preciso ordenar os rows pelo indice 0.
a saida impressa tem que ser row2,row1,row3 para se ter 0.0, 0.5 e 1.0 (sequencial).
Nao estou sabendo fazer esse sort de ArrayList dentro de ArrayList
O método sort() pode receber um objeto que implementa a interface Comparator. Criando uma classe que implementa esta interface, você pode escrever a forma de ordenação que você quiser.
@author paolo
*/
class Compare implements Comparator {
/**
construtor
*/
public Compare() {
}
/**
@param arg0
@param arg1
@return Returns a negative integer, zero, or a positive integer as the
first argument is less than, equal to, or greater than the second.
*/
public int compareDate(Date arg0, Date arg1) {
long l0 = arg0.getTime();
long l1 = arg1.getTime();
if (l0 > l1)
return 1;
if (l0 == l1)
return 0;
return -1;
}
/**
@param arg0
@param arg1
@return Returns a negative integer, zero, or a positive integer as the
first argument is less than, equal to, or greater than the second.
*/
public int compareDouble(Double arg0, Double arg1) {
return arg0.compareTo(arg1);
}
/**
{@inheritDoc}
*/ @Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Double && o2 instanceof Double)
return compareDouble((Double) o1, (Double) o2);
Date d1 = Util.stringToDate((String) o1, Util.DEFAULT_DATE_FORMAT);
Date d2 = Util.stringToDate((String) o2, Util.DEFAULT_DATE_FORMAT);
return compareDate(d1, d2);
}
}[/code]