vasanth
I'm new here

How to Implement Comparator Class in First Spirit ?

Dear All,

I want to sort the entity object for different language. So I have written a separate Comaparator calss in the Module, While calling the Comparator from the template I am getting exception (NullPointerException), but there is no stack trace for that. Is that possible to call the comparator in the module ??

Template Code Snippet:

$CMS_SET(comparator, class("de.espirit.util.comparator.CollatorComparator").instance())$

$CMS_SET(void,class("java.util.Collections").sort(entityList,comparator))$

CollatorComparator is the custom comparator Object

entityList - entity Object List

CollatorComparator.java

public static CollatorComparator getInstance (){

       

       

        return new CollatorComparator();

    }

    @Override

    public int compare(EntityImpl entity1, EntityImpl entity2)

    {

       

        return localeCollator.compare((String)entity1.get(Constants.table_column), (String) entity2

                .get(Constants.table_column));

    }

0 Kudos
4 Replies
maaroufi
I'm new here

Dear Vasanth,

try the following:

<CMS_HEADER>

    <CMS_FUNCTION name="contentSelect" resultname="yourContent">

            <CMS_PARAM name="schema" value="yourSchema"/>

                 <QUERY entityType="yourEntityType">

                </QUERY>

    </CMS_FUNCTION>

</CMS_HEADER>

$CMS_SET(comparator,class("de.espirit.util.comparator.CollatorComparator").instance())$

$CMS_SET(arrayList,class("java.util.ArrayList").new(yourContent))$

$CMS_SET(void,class("java.util.Collections").sort(arrayList,comparator))$

Sincerely yours

Ismail

Peter_Jodeleit
Crownpeak employee

So I have written a separate Comaparator calss in the Module, While calling the Comparator from the template I am getting exception (NullPointerException), but there is no stack trace for that. Is that possible to call the comparator in the module ??

To get a trace you have to switch on debug mode:

     $CMS_SET(#global.debugMode, true)$

Peter
0 Kudos

Thank you Peter

0 Kudos

Thank you Ismail. Its working fine ..

0 Kudos