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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.