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 7842005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:13:17+00:00 2026-06-02T16:13:17+00:00

I am planning to implement server-side sorting and paging for a data table to

  • 0

I am planning to implement server-side sorting and paging for a data table to be shown on the webpage. The data table is a Javascript managed HTML table with external CSS. Data will be pulled from the server-side by Ajax. I am thinking about creating a class on the server-side to represent the data table but have no idea how to implement the sorting on different columns. The sorting should be general enough for any class that backs up the table.

Edit: a backup class such as this:

public class Inventory 
{
   private int itemsLeft = 0;
   private float price = 0.0f;
   private boolean status = false;
   private int itemsSold = 0;

   public int getItemsSold()
   {
       return itemsSold;
   }
   public void setItemsSold(int itemsSold)
   {
       this.itemsSold = itemsSold;
   }
   //... and other getters and setters
}

Each of the private field will be one of the column for the data table and the whole table will be represented as an ArrayList of Inventory. Each instance of the back up class consists of one database table row.

When the header of any sortable column is clicked, the index or name of the column will be sent to the server for sorting data according to the selected column.

I am now stuck at how to make a general sorting function for any back up class. Your suggestions will be much appreciated.

  • 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-02T16:13:18+00:00Added an answer on June 2, 2026 at 4:13 pm

    Thanks to Amir and cuberoot, after some work, I finally come up with this:

    import java.util.Comparator;
    import java.lang.reflect.Method;
    
    public class ObjectComparator<T> implements Comparator<T>{
    
       private String field;
       private String order;
       private Method method;
       private Class<T> cls;
    
       public ObjectComparator(String field, String order, Class<T> c){
           this.field = field;
           this.order = order;
           this.cls = c; 
           init();
       }
    
       private void init(){
           String field_name = "get"+field.substring(0,1).toUpperCase()+field.substring(1);
           try{
              method = cls.getDeclaredMethod(field_name,new Class[]{});
           }
           catch(Exception ex){
              System.err.println("No Such Method Found!");
           }        
       }
       @SuppressWarnings("unchecked")
       public int compare(T o1, T o2) {
            try{    
              Object o1_ = method.invoke(o1,new Object[]{});
              Object o2_ = method.invoke(o2,new Object[]{});
              //Move all objects with null field values
              //to the end of the list regardless of sorting order.
              if(o1_== null) return 1;
              else if(o2_== null) return -1;
               //////////////////////////////////
              if (order.equalsIgnoreCase("asc"))                
                  return ((Comparable<Object>)o1_).compareTo(o2_);    
              else 
                  return ((Comparable<Object>)o2_).compareTo(o1_);   
           }
           catch (Exception ex)
           {
               System.err.println("error sorting");
               return 0;
           }        
       }
    }
    

    This can be used as:

    List<Inventory> list = new ArrayList<Inventory>();//populate list    
    Collections.sort(list, new ObjectComparator<Inventory>(field_name, order, Inventory.class));
    

    Here field_name is the name of the field to be sorted on. It is sent to the server as a request parameter when the header of the table is clicked. There is a requirement with this approach: all the sortable fields in the domain object must implement Comparable interface. Also there must be JavaBean style getter and setter methods for the fields of interests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning to implement Server Push functionality in human workflow. Say when a
we are planning to implement sql server 2005 cluster in next few months. i
I am planning to implement a table checksum mechanism to validate that replication has
I'm planning to implement my own set of constraints, and am having some difficulty
We are planning to implement ACL on our Linux platform. Only one particular group
I am planning to implement spam filter using Naive Bayesian classification model. Online I
we have started research on Service Broker and planning to implement in application. But
Is there a simple Model Checker tool. I am planning to implement a model
I am working on a client-server project and need to implement a logic where
I am planning to implement a small standalone program that will make a https

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.