user3444989
user3444989

Reputation:

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.

Upvotes: 0

Views: 272

Answers (2)

thwe
thwe

Reputation: 1

Why do you need to create Date-Objects?

cmsResource.getDateCreated() returns a long and you can compare it with Long.compare(x,y):

I'd propose this shorter solution:

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

Upvotes: 0

Mohammad Ashfaq
Mohammad Ashfaq

Reputation: 1375

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

Upvotes: 1

Related Questions