1

I want my List will be sorted on the basis of Date variable so i have to write this logic

public  static void main(String a[]){
        AuditLogComponent comp = new AuditLogComponent();
        List<TableRecord> records = new ArrayList<TableRecord>();
        LogFolderRecord log = new LogFolderRecord();
        Date date = new Date();
        Timestamp fromTS1 = new Timestamp(date.getTime());
        log.setStampDate(fromTS1);
        records.add(log);
        Date date1 = new Date();
        Timestamp fromTS2 = new Timestamp(date1.getTime());
        LogFolderRecord log1 = new LogFolderRecord();
        log1.setStampDate(fromTS2);
        records.add(log1);
        records.add(new LogPeopleRecord());

        comp.columnsList(records);
    }

And how i am calling Sort method of List

public void columnsList(List<TableRecord> records){

          Collections.sort(records, new StampDateComparator());
        for(TableRecord record : records){
            Table table = record.getTable();
            Field[] fields = table.fields();
            for(Field field : fields){
                field.getName();
                record.getValue(field);

            }
        }

    }

And my Comparator class is

public class StampDateComparator implements Comparator<Object> {

    @Override
    public int compare(Object object1, Object object2) {

        try {
            Method method = object1.getClass().getDeclaredMethod("getStampDate");
            Date value = (Date) method.invoke(object1);

            Method method1 = object1.getClass().getDeclaredMethod("getStampDate");
            Date value1 = (Date) method.invoke(object2);

            if(value != null && value1 != null )
            return  value.compareTo(value1);;
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }   catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        return 0;

    }

But it not throwing exception

java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.csdcsystems.amanda.comp.macro.StampDateComparator.compare(StampDateComparator.java:22)
    at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)

What is exact way to do Sorting on the basis of Date?

7
  • you need to implement comparator<tablerecord> and not object Commented Sep 25, 2013 at 9:52
  • Take help from this. thejavageek.com/2013/06/17/sorting-user-defined-objects-part-2 Commented Sep 25, 2013 at 9:52
  • Why use reflection? Why not simply use generics as they are suppose to be or cast to the appropriate type (after checking)? Commented Sep 25, 2013 at 9:52
  • Its not possible for me TableRecord is Interface and Classes like LogPeopleRecord,FolderPeopleRecord,FamilyRecord etc. Commented Sep 25, 2013 at 9:53
  • Because its a JOOQ implementation of my code Commented Sep 25, 2013 at 9:54

3 Answers 3

2

Your Comparator class should look like

public class StampDateComparator implements Comparator<TableRecord> {

    @Override
    public int compare(TableRecord record1, TableRecord record2) {
        return record1.getStampDate().compareTo(record2.getStampDate());
    }
}

EDIT: Even if TableRecord is an interface, you can still use it in your Compartor, all your implementation of TableRecord should have the method getStampDate correctly implemented.

Sign up to request clarification or add additional context in comments.

3 Comments

Tablerecord Does not contain StamDate its a Interface
then you need to revisit your design, and then understand why you wouldn't want to have getStampDate in your TableRecord interface. Because it doesn't makes sense to put some random objects in your List
TableRecord Interfaces given by JOOQ API its not my class and its not random object its implements TableRecord class
0

In Comparator class, at first you need to get Timestamp from TableRecord class. Timestamp has its compareTo method to sort it. Design your StampDateComparator class like:

public class StampDateComparator implements Comparator<TableRecord> {

    @Override
    public int compare(TableRecord record1, TableRecord record2) {
        Timestamp timestamp1=record1.getStampDate();        
        Timestamp timestamp2=record2.getStampDate();
        return timestamp1.compareTo(timestamp2);
    }
}

Comments

0

the method signatures of getDeclaredMethod and invoke are wrong:

Method method = object1.getClass().getDeclaredMethod("getStampDate", null);
Date value = (Date) method.invoke(object1, null);

Method method1 = object2.getClass().getDeclaredMethod("getStampDate", null);
Date value1 = (Date) method.invoke(object2, null);

They require the parameters for the method. See JavaDoc:

6 Comments

If this is wrong it should give Compile time exception to me
what jdk are you using?
could you please try if my code works? i don't get, why you don't get a compilation error
I made a mistake i called this object1.getClass().getDeclaredMethod("getStampDate"); two time while it would Object2 also method varaible creating issue that also resolved
but the class of object1 and object2 should be the same so that doesn't matter
|