1

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.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Ssv
  • 1,059
  • 4
  • 14
  • 25

1 Answers1

6

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 or 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:

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102