Raw DataTable data is not sorted properly

I am a Java EE web application developer (spring, hibernate, jsf, primefaces), and I found a problem with the DataTable component components. The problem is with sorting columns, especially sorting words with special characters.

In my language (Czech), we use characters such as (č, Ε™, ΕΎ, etc.), and words starting with these characters are sorted at the end of the table. And that is the problem. They should be sorted after the corresponding letter, for example. "č" should be after "c", "Ε™" should be after "r", etc., and not after all entries without special characters.

I already use the CharacterEncoding filter provided by the Spring Framework, which should force encoding (UTF-8) for each request and response. But this does not solve the problem. Here is the filter configuration:

<filter> <filter-name>charEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> 

Is there any way to fix this behavior?

+6
source share
1 answer

I hope this is as useful as it is for you. entities here is just a List<String> :

 <p:dataTable value="#{testBean.entities}" var="ent"> <p:column headerText="..." sortBy="#{ent}" sortFunction="#{testBean.sort}"> #{ent} </p:column> </p:dataTable> 

Bean Method:

 public int sort(Object ent1, Object ent2) { String s1 = (String) ent1; String s2 = (String) ent2; Collator collator = Collator.getInstance(new Locale("cs")); //Your locale here collator.setStrength(Collator.IDENTICAL); return collator.compare(s1, s2); } 

Of course, a Collator can be assigned to a bean property for some performance.

If this is only the default sort, you simply move above sortBy and sortFunction to p:datatable -tag.

+5
source

Source: https://habr.com/ru/post/985381/


All Articles