Search code examples
javasortingcollectionsopencms

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


I am working on OpenCms, a Java-based CMS. I have a List collection in which I am storing CMSResources which I get from a category like:

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,"/");

Now, I have written some code to sort it like:

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

When I loop through the collection using Iterator then the order is not correct. It should be ordered according to date, ascending or descending, as I am trying to do with the Comparator.

I have iterated through using:

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

Please can anybody help me.


Solution

  • Try this one, it should resolve your problem:

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