Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1240
  • Last Modified:

How to sort the paginated data in ascending and Descending order


Thanks for guiding me by giving answers to my questions.

I want to display the paginated data in ascending and descending order.

Can anybody give guidance on this, sothat i can  complete my assignment.

Thanks in advance..

2 Solutions
you are going to paginate some list or arraylist , hence first sort that list based on ascending order or descending order, and then display the result using pagination. Dont mix pagination and ordering. you can also do ordering at the database level(in the select query) and also at the business logic level if you know what kind of ordering is needed..
Slightly not clear. Do you want to sort the paginated data in your display?
Or do you want to display an already sorted data on the page? How do you control
whether you want ascending or descending order?

Please clarify those doubts and I shall help you out with this.

Hi All

               int nRecords =0, nPages =0, nFrom =0, nTo =0, nCurrentPage =1, nRecordsPerPage =25;  

        // 0- Get Actual Current Page
          nCurrentPage = Integer.parseInt(request.getParameter("nCurrentPage"));  
                nCurrentPage = 1;

     // 1- get the Number of the Records
     rs = statement.executeQuery("SELECT  count(*) as nRec FROM viajes");
     nRecords = rs.getInt("nRec");
     // 2- get total pages
     nPages= (nRecords/nRecordsPerPage) + 1;
     // 3- set Page Starting Record
     nFrom = (nCurrentPage-1) * nRecordsPerPage ;
        // 4- Show Rows
     rs = statement.executeQuery("SELECT viajes.ID, viajes.titulo FROM viajes where GROUP BY ID DESC LIMIT "+ nFrom + "," + nRecordsPerPage );    


and for Next and Previous Buttons

*) Previous Button
                         <% if (nCurrentPage>1) {%><a href="ofertas.jsp?nCurrentPage=<%=(nCurrentPage-1)%>"></a><%}%>

*) Next Button
                        <% if (nPages > nCurrentPage) {%><a href="ofertas.jsp?nCurrentPage=<%=(nCurrentPage-1)%>"></a><%}%>

*** in this exampled i sorted the records accordin to ID look at the query statement

Khaled Ahmed

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now