How to sort the paginated data in ascending and Descending order

Posted on 2006-03-23
Last Modified: 2012-06-27

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

Question by:murali
    LVL 3

    Accepted Solution

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

    Expert Comment

    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.


    Assisted Solution

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Stuck in Bootstrap WysiHtml Editor 3 33
    spring AOP 6 64
    J2SE 5.0 metadata 1 40
    eclipse luna javeEE perspecive missing 5 61
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    It Is not possible to enable LLDP in vSwitch(at least is not supported by VMware), so in this article we will enable this, and also go trough how to enabled CDP and how to get this information in vSwitches and also in vDS.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now