1
votes

I am trying to implement a sorting function on a PrimeFaces dataTable. I have created a list of model objects. We have an issue with sorting order of the table.The sorting column contains integers and string values. When I apply default sort mechanism like sort=#{var.id} the list was sorting based on ASCII order. Below is the image of the sorting result. I'm currently using PrimeFaces v5.2.5

<p:column headerText="Code Type" sortBy="#{var.codeType}">
    <h:outputText value="#{var.codeType}"/>
</p:column>

enter image description here

Can anybody guide me how to overcome this problem.

1
You are ging for "1", "2", "3" .. "10", "11" .. "20" .. "100" .. "Aaa", "Aab" .. "Zzz", right? - Jasper de Vries
@Jasper: yes, you are correct. - Ssv
OK. Updated my answer. Added link to stackoverflow.com/questions/104599/… - Jasper de Vries

1 Answers

4
votes

There are two options here:

  1. Use sortFunction on your p:column
  2. Use a type which implements Comparable

sortFunction

Create a function which takes two objects and compare them in the way comparable would:

public int sortByModel(Object o1, Object o2) {
    //return -1, 0 , 1 if o1 is less than, equal to or greater than o2
}

And use it in your column:

<p:column sortBy="#{var.codeType}" sortFunction="#{yourBean.sortByModel}">

Comparable type

Use a custom type for your column and make sure that it implements Comparable<YourCustomType>. This forces you to implement the compareTo(YourCustomType o) method which you can use to sort any way you like.

See also: