簡體   English   中英

無法通過OpenCms中的升序或降序對列表集合進行排序

[英]Unable to sort List Collection by ascending or desending order in OpenCms

我正在使用基於Java的CMS OpenCms。 我有一個列表集合,其中存儲着我從類似類別中獲得的CMSResources:

CmsObject cmso = cms.getCmsObject();

// Get the category service instance
CmsCategoryService cs = CmsCategoryService.getInstance();

// Get resources assigned a specific category. (You could also provide a CmsResourceFilter here.)
List<CmsResource> categoryResourcesNewBies = cs.readCategoryResources(cmso, "newbies/", true,"/");

現在,我編寫了一些代碼來對其進行排序:

 final SimpleDateFormat outputFormat = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz");
 final Comparator<CmsResource> CREATE_ORDER = new Comparator<CmsResource>() {
     public int compare(CmsResource one, CmsResource another) {
         String oneCreated = "";
         String anotherCreated = "";
         try {
             oneCreated = outputFormat.format(new Date(one.getDateCreated()));
             anotherCreated = outputFormat.format(new Date(another.getDateCreated()));
         } catch (Exception e) {
             oneCreated = "";
             anotherCreated = "";
         }
         return oneCreated.compareTo(anotherCreated);
     }
 };
 Collections.sort(categoryResourcesNewBies,CREATE_ORDER);

當我使用Iterator遍歷集合時,順序不正確。 正如我嘗試使用比較器一樣,應根據日期升序或降序進行排序。

我已經通過使用迭代:

Iterator<CmsResource> inew = categoryResourcesNewBies.iterator();
while (inew.hasNext()) {
    CmsResource r = inew.next();

    // Here, you could check the file type ID and/or file extension, and do something
    // based on that info. For example, you could group PDF and DOC files separately, or
    // discard all files other than PDFs, and so on.

    String urinew = cmso.getSitePath(r);
    <li class="margin-bottom-10 left-nav-list"><a href="<%= cms.link(urinew) %>" target="_blank"><%=        cms.property(CmsPropertyDefinition.PROPERTY_TITLE, urinew, urinew) %><img src="/.content/flexiblecontents/img/new.gif" alt = "New"/>  <%out.println(outputFormat.format(r.getDateCreated()));%></a></li>
}

請任何人能幫助我。

試試這個,它應該可以解決您的問題:

 final Comparator<CmsResource> CREATE_ORDER = new Comparator<CmsResource>() {
     public int compare(CmsResource one, CmsResource another) {
         Date oneCreated = null;
         Date anotherCreated = null;
         try {
             oneCreated = new Date(one.getDateCreated());
             anotherCreated = new Date(another.getDateCreated());
         } catch (Exception e) {
             oneCreated = null;
             anotherCreated = null;
             return 0;
         }
         if ( oneCreated.after(anotherCreated))
            return 1;
         else if ( oneCreated.before(anotherCreated))
            return -1;
         else
            return 0;
     }
 };
 Collections.sort(categoryResourcesNewBies,CREATE_ORDER);

為什么需要創建日期對象?

cmsResource.getDateCreated()返回一個long,可以將其與Long.compare(x,y)進行比較:

我建議這個較短的解決方案:

public int compare(CmsResource one, CmsResource another) {
    try {
        return Long.compare(one.getDateCreated(),another.getDateCreated());
    } catch (Exception e) {
        return 0;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM