2014-09-22 114 views
-1

一切都很好,但我想在LOCATIONNAME排序順序,locationMasterList有整數,字符串和Double對象.. 下面是我的代碼...名單按字母順序排序

List<LocationMaster> locationMasterList = locationMasterService.findByProperty("locationId", locationId); 


for (int n = 0; n < locationMasterList.size(); n++) { 
    YearwiseBudget yearwiseBudget1 = new YearwiseBudget(); 
    String locationName = locationMasterList.get(n).getLocationNameE(); 
    Integer distId = locationMasterList.get(n).getLocationId(); 
    yearwiseBudget1.setRecordId(recordId.longValue()); 
    yearwiseBudget1.setAllocation(allocation); 
    yearwiseBudget1.setExpenditure(expenditure); 
    yearwiseBudget1.setLocationName(locationName); 
    yearwiseBudget1.setAllocGen(allocGen); 
    yearwiseBudget1.setAllocSC(allocSC); 
    yearwiseBudget1.setAllocST(allocST); 
    yearwiseBudget1.setExpGen(expGen); 
    yearwiseBudget1.setExpSC(expSC); 
    yearwiseBudget1.setExpST(expST); 
    yearwiseBudget1.setDistrictId(distId); 
    YearwiseBudgetList2.add(yearwiseBudget1); 
} 
+1

你可以爲'POJO'實現'Comparable'接口,然後你可以使用'Collections.sort(YourPojo)'。 – AknKplnoglu 2014-09-22 10:06:08

回答

0

YearlyBudget實施Comparable和比較locationName的成員。

public class YearwiseBudget implements Comparable<YearwiseBudget> { 
    private String locationName; 
    . 
    . 
    . 

    @Override 
    public int compareTo(YearwiseBudget other) { 
     return other.locationName.compareTo(locationName); 
    } 

} 

然後只是在您的清單上使用Collections.sort(List<T> list)

+0

感謝您的幫助..... @ dassimon,@ AknKplnoglu – 2014-09-23 12:14:05

+0

沒問題@Surendra Mishra。 Upvote任何你覺得有用的東西。 – dassimon 2014-09-24 14:04:20