2013-09-24 52 views
0

我得到一個問題,我有下面的代碼Java ArrayList在動態對象字段的基礎上排序?

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變量,它是在每個類如何對它們進行排序我們可以做到這一點,當我們在列表中的不同類別

回答

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); 
    } 
} 

} 
+0

由於名譽較低,我不能投票你的問題 – Hariom

+0

@Hariom但你可以把它標記爲正確答案;-)。無論如何感謝:-) – agad

+0

是的,我會再次提出一個問題,我必須根據日期來排序,這段代碼會執行 – Hariom

1

只要寫抽象類記錄與保護地stampDate,實現可比較的並覆蓋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{ 
... 
} 
+0

您能否簡要地與幾行代碼 – Hariom

+0

不太正確解釋。[比較](http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html)是在這種情況下更合適,因爲[Comparable](http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html)強加了「自然順序」。 – aUserHimself

+0

@aUser自己,也許這不太對,但這個例子仍然可用。讓TS選擇=) –

2

如果你上面的代碼是正確的,這意味着AddressRecordCityRecordUserRecord所有擴展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); 
    } 
} 
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. 
    } 

} 
+0

'else if(TableRecord inteseof CityRecord)'在這裏是不可能的,可以是100我必須比較的類 – Hariom