0

我遇到了以下代码的问题

public void columnsList(List<TableRecord> records){

    for(TableRecord record : records){
        Table table = record.getTable();
        //Do sort here on stampDate
        Field[] fields = table.fields();
        for(Field field : fields){
            field.getName();
            record.getValue(field);

        }
    }

}

records对象包含不同类类型的对象

List<TableRecord> records = new List<TableRecord>();
records.add(new AddressRecord());
records.add(new CityRecord());
records.add(new UserRecord());

现在我需要如何stampDate按每个类中的变量对它们进行排序,当我们在列表中有不同的类时,我们该怎么做

4

4 回答 4

2

如果您上面的代码是正确的,这意味着AddressRecord,CityRecord并且UserRecord全部扩展TableRecord

class AddressRecord extends TableRecord {
    // other fields and methods here
}
class CityRecord extends TableRecord {
    // other fields and methods here
}
class UserRecord extends TableRecord {
    // other fields and methods here
}

你只需要Comparator为这门课写一个。它应该看起来像这样:

class TableRecord {
    private Date timeStamp;

    public Date getTimeStamp() {
        return timeStamp;
    }
// other fields and methods here
}

class RecordStampDateComparator implements Comparator<TableRecord>{

    public int compare(TableRecord tr1, TableRecord tr2) {
        Date tr1Date = tr1.getTimeStamp();
        Date tr2Date = tr2.getTimeStamp();   
        return tr1Date.compareTo(tr2Date);
    }
}
于 2013-09-24T06:54:14.120 回答
1

只需编写带有受保护字段stampDate的抽象类Record,实现Comparable并覆盖compareTo方法。

public abstract class Record implements Comparable<Record> {
    protected Date stampDate;

    @Override
    public int compareTo(Record anotherRecord){
        return this.stampDate.compareTo(anotherRecord.stampDate);
    }
}

然后用你的记录类扩展这个类:

public class AddressRecord extends Record{
...
}

public class CityRecord extends Record{
...
}

public class UserRecord extends Record{
...
}
于 2013-09-24T06:50:41.803 回答
1

如果您无法更改类,请编写您的比较器(Comparator<Object>),它将尝试查找字段 stampDate 并比较它们。比用它来排序列表。比较器实现:

import java.util.Comparator;
import java.util.Date;


public class StampDateComparator implements Comparator<Object> {

@Override
public int compare(Object o1, Object o2) {

    try {
        Date d1 = (Date) o1.getClass().getDeclaredField("stampDate").get(o1);
        Date d2 = (Date) o2.getClass().getDeclaredField("stampDate").get(o2);
        return compare(d1, d2);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException("Missing variable stampDate");
    }catch (ClassCastException e) {
        throw new RuntimeException("stampDate is not a Date");
    } catch (IllegalArgumentException e) {
        //shoud not happen
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

}
于 2013-09-24T06:54:59.747 回答
0

在 List 上使用以下 Comparator 类。

class TableRecordCompare implements Comparator<TableRecord>{
     if(TableRecord instanceof AddressRecord){
       // return compareTo for sample data of address.
     }
     else if(TableRecord instanceof CityRecord){
     // return compareTo for sample data of CityRecord.
      }
     else{
     // return compareTo for sample data of UserRecord.
    }

}
于 2013-09-24T06:55:40.513 回答