Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:18:31+00:00 2026-06-08T14:18:31+00:00

is it possible sort primefaces datagrid data? as i’m aware it’s possible in data

  • 0

is it possible sort primefaces datagrid data? as i’m aware it’s possible in data table.if it is not possible by default,is there any other way to do it? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:18:33+00:00Added an answer on June 8, 2026 at 2:18 pm

    Primefaces datagrid does not provide sorting nor filtering, and it seems that there are no plans for implementing it anytime soon.
    I’ve implemented sorting in a datagrid using a p:selectOneMenu just above the datagrid with sorting options like “Date”, “Price (highest first)”, “Price (lowest first)”, etc. When the selected value changes, I use an ajax event to invoke a method that reloads the collection from database using the appropiate ORDER BY.

    Example:

    <h:form>
        <h:outputLabel value="Sort by: " />
        <p:selectOneMenu value="#{bean.selectedField}" >
            <f:selectItems value="#{bean.sortingFields}" />
            <p:ajax event="change" update="grid" />
        </p:selectOneMenu>
    
        <p:dataGrid id="grid" value="#{bean.collection}" var="item" >
                <!-- contents of datagrid here -->
        </p:dataGrid>
    </h:form>
    

    And in the managed bean:

    private List<SelectItem> sortingFieds;
    private String selectedField, currentSortField;
    private boolean asc;
    private List<YourEntity> collection;
    @EJB private YourEJB yourEjb;
    
    public void init() {
        // load sortingFields with list of fields to order by
        // set default value for currentSortField
        asc = true;
        collection = yourEjb.loadCollection(sortField, asc);
    }
    
    public void sortCollection() {
        if(currentSortField.equals(selectedField) {
            asc = !asc;
        } else {
            currentSortField = selectedField;
            asc = true;
        }
        collection = yourEjb.loadCollection(sortField, asc);
    }
    

    And in your ejb

    public List<YourEntity> loadCollection(String sortfield, boolean asc) {
        String q = "SELECT e FROM YourEntity e ORDER BY " + sortfield + " ";
        if(asc) {
           q += "ASC";
         } else {
           q += "DESC";
        }
        Query query = entityManager.createQuery(q);
        return query.getResultList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How is it possible to sort data from multiple tables by relevance? My table
Is it possible to sort the data inside of an oracle table? Like ascending/descending
Possible Duplicate: Default sort-ordering in MySQL (ALTER TABLE … ORDER BY …;) I have
Is it possible to sort a TreeView collection using IComparer<T> and not IComparer ?
I'm trying to sort a HashMap in two ways. The default way: alphabetically by
Possible Duplicate: Sort a list of tuples by their second elements Hey there i
Is it possible to sort the elements and not only the nodes using XPathExpression.AddSort?
is it possible to simply sort a asp:Table by columns?
Is it possible to sort results from Core Data based on the entity type,
is it possible to sort div by using the data function? html <div id=gp_21

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.