We help IT Professionals succeed at work.

How to sort the paginated data in ascending and Descending order

murali
murali asked
on
Medium Priority
1,326 Views
Last Modified: 2012-06-27
Hi..

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..

Comment
Watch Question

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..

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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.

________
radarsh
Commented:
Hi All


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

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

     
     // 1- get the Number of the Records
     rs = statement.executeQuery("SELECT  count(*) as nRec FROM viajes");
     rs.next();
     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
     
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.